Podstrony
- Strona startowa
- Stalo sie jutro Zbior 24
- (ebook pdf) Teach Yourself SQL in 21 Days
- Teach Yourself Visual C 6 in 21 days
- Teach Yourself DirectX 7 in 24 Hours (2)
- Teach Yourself Linux in 24 Hours (2)
- teach yourself linux in 24 hours
- Administrator Linux (3)
- balzac honoriusz komedia ludzka vi
- Le Guin, Ursula K Tehanu
- Stephen King To (rtf)
- zanotowane.pl
- doc.pisz.pl
- pdf.pisz.pl
- fruttidimare.keep.pl
[ Pobierz całość w formacie PDF ]
.Second,an array allows you to work easily with all its items.You can loop through each itemor pull one out at random.You can sort items numerically, alphabetically, or evenaccording to a system of your own.Each item in an array is commonly referred to as an element.Each element can beaccessed directly via its index.An index to an array element can be either a numberor a string.By default, array elements are indexed by number starting at zero.It's important toremember, therefore, that the index of the last element of a numerically indexed109array is always one less than the number of elements the array contains.For example, Table 7.1 shows the elements in an array called users.Notice that thethird element has an index of 2.Table 7.1: The Elements in the users ArrayIndex Value WhichNumber Element?0 Bert First1 Sharon Second2 Betty Third3 Harry FourthIndexing arrays by string can be useful in cases where you need to store both namesand values.PHP4 provides tools to access and manipulate arrays indexed by both name andnumber.Some of these are covered in this hour, and others will be covered in Hour16, "Working with Data."Creating ArraysBy default, arrays are lists of values indexed by number.Values can be assigned toan array in two ways: with the array() function or directly using the array identifier[].You'll meet both of these in the next two sections.Defining Arrays with the array() FunctionThe array() function is useful when you want to assign multiple values to an array atone time.Let's define an array called $users and assign four strings to it:$users = array ("Bert", "Sharon", "Betty", "Harry" );You can now access the third element in the $user array by using the index "2":print "$users[2]";This would return the string "Sharon".The index of an array element is placedbetween square brackets directly after the array name.You can use this notationeither to set or retrieve a value.Remember that arrays are indexed from zero by default, so the index of anyelement always is one less than the element's place in the list.110Defining or Adding to Arrays with the Array IdentifierYou can create a new array (or add to an existing one) by using the array identifierin conjunction with the array name.The array identifier is a set of square bracketswith no index number or name inside it.Let's re-create our $users array in this way:$users[ ] = " Bert";$users[ ] = " Sharon";$users[ ] = " Betty";$users[ ] = " Harry";Notice that we didn't need to place any numbers between the square brackets.PHP4automatically takes care of the index number, which saves you from having to workout which is the next available slot.We could have added numbers if we wanted, and the result would have been exactlythe same.It's not advisable to do this, though.Take a look at the following code:$users[0] = " Bert";$users[200] = "Sharon";The array has only two elements, but the index of the final element is 200.PHP4 willnot initialize the intervening elements.This could lead to confusion when attemptingto access elements in the array.In addition to creating arrays, you can use the array identifier to add new valuesonto the end of an existing array.In the following code, we define an array with thearray() function and use the array identifier to add a new element:$users = array ("Bert", " Sharon", "Betty", "Harry" );$users[] = "sally";Associative ArraysNumerically indexed arrays are useful for storing values in the order in which theywere added or according to a sort pattern.Sometimes, though, you need to accesselements in an array by name.An associative array is indexed with strings between111the square brackets rather than numbers.Imagine an address book.Which wouldbe easier, indexing the "name" field as 4 or as "name"?NEW Arrays indexed by strings are known as associative arrays.YouTERM may also see them referred to as hashes.Once again, you can define an associative array using either array() or the arrayidentifier [].Tip The division between an associative array and a numerically indexedarray is not absolute in PHP.They are not separate types as arrays andhashes are in Perl.It is a good idea, nevertheless, to treat themseparately.Each demands different strategies for access andmanipulation.Defining Associative Arrays with the array() FunctionTo define an associative array with the array() function, you must define both thekey and value for each element
[ Pobierz całość w formacie PDF ]