diff --git a/language/types.xml b/language/types.xml index f90e1dcbf1..d02b43d302 100644 --- a/language/types.xml +++ b/language/types.xml @@ -1,5 +1,5 @@ - + Types @@ -1239,12 +1239,12 @@ echo "\$foo==$foo; type is " . gettype ($foo) . "
\n"; hashtable (which is an implementation of a map), dictionary, collection, stack, queue and probably more. Because you can have another - PHP-array as a value, you can also quite easily simulate + PHP array as a value, you can also quite easily simulate trees. - Explanation of those structures is beyond the scope of this manual, - but you'll find at least one example for each of those structures. + Explanation of those data structures is beyond the scope of this + manual, but you'll find at least one example for each of them. For more information we refer you to external literature about this broad topic. @@ -1263,13 +1263,11 @@ echo "\$foo==$foo; type is " . gettype ($foo) . "
\n"; -array( key => value +array( key => value , ... ) -// key is either string or nonnegative integer -// value can be anything +// key may be an integer or string +// value may be any value @@ -1287,14 +1285,14 @@ echo $arr[12]; // 1 - A key is either an integer - or a string. If a key is the standard representation - of an integer, it will be interpreted as such (i.e. - "8" will be interpreted as 8, - while "08" will be interpreted as - "08"). There are no different indexed and - associative array types in PHP, there is only one array type, - which can both contain integer and string indices. + A key may be either an integer + string. If a key is the standard representation of + an integer, it will be interpreted as such (i.e. + "8" will be interpreted as + 8, while "08" will be + interpreted as "08"). There are no different + indexed and associative array types in PHP; there is only one + array type, which can both contain integer and string indices. A value can be of any PHP type. @@ -1313,13 +1311,14 @@ echo $arr["somearray"]["a"]; // 42 - If you omit a key, the maximum of the integer-indices is taken, and - the new key will be that maximum + 1. As integers can be negative, - this is also true for negative indices. Having e.g. the highest index - being -6 will result in -5 being - the new key. If no integer-indices exist - yet, the key will be 0 (zero). If you specify a key - that already has a value assigned to it, that value will be overwritten. + If you provide the brackets without specifying a key, then the + maximum of the integer indices is taken, and the new key will be + that maximum value + 1--unless that maximum value is negative + (is it perfectly legal to have negative array indices). In this + case, the new key will be 0. If no integer + indices exist yet, the key will be 0 + (zero). If you specify a key that already has a value assigned + to it, that value will be overwritten. 43, 6 => 32, 7 => 56, "b" => 12); - Using &true; as a key will evalute to - integer 1 as key. Using - &false; as a key will evalute to integer - 0 as key. Using NULL as a key - will evaluate to an empty string. Using an emptry string as key will - create (or overwrite) a key with an empty string and its value, it is - not the same as using empty brackets. + Using &true; as a key will evaluate to integer + 1 as key. Using &false; as a key will evaluate + to integer 0 as key. Using + NULL as a key will evaluate to the empty + string. Using the empty string as key will create (or overwrite) + a key with the empty string and its value; it is not the same as + using empty brackets. You cannot use arrays or objects as keys. Doing so will result in a @@ -1352,19 +1351,18 @@ array(5 => 43, 6 => 32, 7 => 56, "b" => 12); Creating/modifying with square-bracket syntax - You can also modify an existing array, by explicitly setting + You can also modify an existing array by explicitly setting values in it. This is done by assigning values to the array while specifying the key in brackets. You can also omit the key, add an empty pair - of brackets ("[]") to the variable-name in that case. + of brackets ("[]") to the variable name in that case. $arr[key] = value; $arr[] = value; -// key is either string or nonnegative integer -// value can be anything +// key may be an integer or string +// value may be any value If $arr doesn't exist yet, it will be created. So this is also an alternative way to specify an array. @@ -1393,23 +1391,78 @@ unset($arr); // This deletes the whole array - If you omit a key, the maximum of the integer-indices is taken, and - the new key will be that maximum + 1. - Note that the maximum is same when the element having - maximum-index was cleared using unset function. - - + As mentioned above, if you provide the brackets with no key + specified, then the maximum of the existing integer indices is + taken, and the new key will be that maximum value + 1--unless + that maximum value is negative (is it perfectly legal to have + negative array indices). In this case, the new key will be + 0. If no integer indices exist yet, the key + will be 0 (zero). If you specify a key that + already has a value assigned to it, that value will be + overwritten. + + + Note that the maximum integer key used for this need + not currently exist in the array. It simply must + have existed in the array at some time since the last time the + array was re-indexed. The following example illustrates: + + + + 'orange', 1 => 'apple', 3 => 'carrot') +// Create a simple array. +$array = array(1, 2, 3, 4, 5); +print_r($array); + +// Now delete every item, but leave the array itself intact: +foreach ($array as $i => $value) { + unset($array[$i]); +} +print_r($array); + +// Append an item (note that the new key is 5, instead of 0 as you +// might expect). +$array[] = 6; +print_r($array); + +// Re-index: +$array = array_values($array); +$array[] = 7; +print_r($array); ?> ]]> - - - + + + The above example would produce the following output: + + 1 + [1] => 2 + [2] => 3 + [3] => 4 + [4] => 5 +) +Array +( +) +Array +( + [5] => 6 +) +Array +( + [0] => 6 + [1] => 7 +) +]]> + + + + @@ -1417,9 +1470,8 @@ var_dump($a); // output: array(0 => 'orange', 1 => 'apple', 3 => 'carrot') Useful functions - There are quite some useful function for working - with arrays, see the array - functions section. + There are quite a few useful functions for working with arrays. + See the array functions section. @@ -1826,7 +1878,7 @@ closedir($handle); count function. - Sorting array + Sorting an array - Because the value of an array can be everything, it can also be + Because the value of an array can be anything, it can also be another array. This way you can make recursive and multi-dimensional arrays.