Podobne
- Strona startowa
- newsweek 24 2006
- (ebook pdf) Teach Yourself Database Programming with Visual C in 21 days
- (ebook pdf) Teach Yourself SQL in 21 Days
- Teach Yourself Database Programming with Visual C in 21 Da
- Teach Yourself DirectX 7 in 24 Hours
- Sams' Teach Yourself Linux In 24 Hours
- Van Roy Peter, Haridi Seif Concepts, Techniques, and Models of Computer Programming
- Jan III Sobieski Listy do Marysienki (2)
- Silva Daniel Ostatni szpieg Hitlera
- Jablonski Witold Dzieci Nocy
- zanotowane.pl
- doc.pisz.pl
- pdf.pisz.pl
- thelemisticum.opx.pl
Cytat
Do celu tam się wysiada. Lec Stanisław Jerzy (pierw. de Tusch-Letz, 1909-1966)
A bogowie grają w kości i nie pytają wcale czy chcesz przyłączyć się do gry (. . . ) Bogowie kpią sobie z twojego poukładanego życia (. . . ) nie przejmują się zbytnio ani naszymi planami na przyszłość ani oczekiwaniami. Gdzieś we wszechświecie rzucają kości i przypadkiem wypada twoja kolej. I odtąd zwyciężyć lub przegrać - to tylko kwestia szczęścia. Borys Pasternak
Idąc po kurzych jajach nie podskakuj. Przysłowie szkockie
I Herkules nie poradzi przeciwko wielu.
Dialog półinteligentów równa się monologowi ćwierćinteligenta. Stanisław Jerzy Lec (pierw. de Tusch - Letz, 1909-1966)
[ Pobierz całość w formacie PDF ]
.Directly Defining or Adding to an Associative ArrayYou can create or add a name/value pair to an associative array simply by assigninga value to a named element.In the following, we re-create our $character array bydirectly assigning a value to each key:112$character[name] = "bob";$character[occupation] = "superhero";$character[age] = 30;$character["special power"] = "x-ray vision";Multidimensional ArraysUntil now, we've simply said that elements of arrays are values.In our $characterarray, three of the elements held strings, and one held an integer.The reality is alittle more complex, however.In fact, an element of an array could be a value, anobject, or even another array.A multidimensional array is an array of arrays.Imagine an array that stores an array in each of its elements.To access the thirdelement of the second element, you would have to use two indices:$array[1][2]NEW Arrays that contain arrays as their elements are known asTERM multidimensional arrays.The fact that an array element can itself be an array enables you to createsophisticated data structures relatively easily.7.1 defines an array that has anassociative array as each of its elements.Listing 7.1: Defining a Multidimensional Array1:2:3: Listing 7.14:5:6: "bob",9: occupation=>"superhero",10: age=>30,11: specialty=>"x-ray vision" ),12: array ( name=>"sally",13: occupation=>"superhero",14: age=>24,15: specialty=>"superhuman strength" ),16: array ( name=>"mary",17: occupation=>"arch villain",18: age=>63,19: specialty=>"nanotechnology" )11320: );21:22: print $characters[0][occupation];23: // prints "superhero"24: ?>25:26:Notice that we have nested array function calls within an array function call.At thefirst level, we define an array.For each of its elements, we define an associativearray.Accessing $user[2], therefore, gives us access to the third associative array in thetop-level array.We can then go ahead and access any of the associative array'sfields.$user[2][name] will be "mary", and $user[2][age] will be 63.When this concept is clear, it will be easy to create complex combinations ofassociative and numerically indexed arrays.Accessing ArraysSo far, you've seen the ways in which you can create and add to arrays.In thissection, you will examine some of the tools that PHP4 provides to allow you toacquire information about arrays and access their elements.Getting the Size of an ArrayYou can access an element of an array by using its index:print $user[4]Because of the flexibility of arrays, however, you won't always know how manyelements a particular array contains.That's where the count() function comes intoplay.count() returns the number of elements in an array.In the following code, wedefine a numerically indexed array and use count() to access its last element:$users = array ("Bert", "Sharon", "Betty", "Harry" );print $users[count($users)-1];Notice that we subtract 1 from the value returned by count().This is because count()returns the number of elements in an array, not the index of the last element.114Although arrays are indexed from zero by default, it is possible to change this.Forthe sake of clarity and consistency, however, this is not usually advisable.Looping Through an ArrayThere are many ways of looping through each element of an array.For theseexamples, you'll use PHP4's powerful foreach statement.You will examine someother methods inHour 16.Note The foreach statement was introduced with PHP4.In the context of numerically indexed arrays, you would use a foreach statementlike this:foreach( $array as $temp ){//.}where $array is the array you want to loop through, and $temp is a variable in whichyou will temporarily store each element.In the following code, we define a numerically indexed array and use foreach toaccess each of its elements in turn:$users = array ("Bert", "Sharon", "Betty", "Harry" );foreach ( $users as $val ){print "$val";}You can see the output from this code fragment in Figure 7.1.115Figure 7.1: Looping through an array.The value of each element is temporarily placed in the variable $val, which we thenprint to the browser.If you are moving to PHP4 from Perl, be aware of a significantdifference in the behavior of foreach.Changing the value of the temporary variablein a Perl foreach loop changes the corresponding element in the array.Changing thetemporary variable in the preceding example would have no effect on the $usersarray
[ Pobierz całość w formacie PDF ]