diff --git a/functions/array.xml b/functions/array.xml index 7f14134997..bb25007293 100644 --- a/functions/array.xml +++ b/functions/array.xml @@ -1,5 +1,5 @@ - + Array Functions Arrays @@ -140,7 +140,7 @@ Array See also array_pad, - list, and range. + list andrange. @@ -308,9 +308,22 @@ Array 2, "hello"=>2, "world"=>1) +print_r(array_count_values ($array)); ]]> + + The printout of the above program will be: + + 2 + [hello] => 2 + [world] => 1 +) +]]> + + @@ -417,18 +430,36 @@ function even($var) { $array1 = array ("a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5); $array2 = array (6, 7, 8, 9, 10, 11, 12); -$odd_arr = array_filter($array1, "odd"); -$even_arr = array_filter($array2, "even"); +echo "Odd :\n"; +print_r(array_filter($array1, "odd")); +echo "Even:\n"; +print_r(array_filter($array2, "even")); ]]> + + The printout of the program above will be: + + 1 + [c] => 3 + [e] => 5 +) +Even: +Array +( + [0] => 6 + [2] => 8 + [4] => 10 + [6] => 12 +) +]]> + + - - This makes $odd_arr have - array ("a"=>1, "c"=>3, "e"=>5);, - and $even_arr have - array (6, 8, 10, 12);, - ¬e.func-callback; See also array_map, and @@ -488,9 +519,21 @@ $original = strtr ($str, $trans); 1, "b" => 1, "c" => 2); $trans = array_flip ($trans); -// now $trans is : array(1 => "b", 2 => "c"); +print_r($trans); ]]> + + now $trans is : + + b + [2] => c +) +]]> + + @@ -523,19 +566,24 @@ $trans = array_flip ($trans); + + $a now has the following entries using print_r: + + banana + [6] => banana + [7] => banana + [8] => banana + [9] => banana + [10] => banana +) +]]> + + @@ -574,12 +622,20 @@ $array2 = array ("b" => "green", "yellow", "red"); $result = array_intersect ($array1, $array2); ]]> + + This makes $result have + + green + [0] => red +) +]]> + + - - This makes $result have array ("a" - => "green", "red"); - Two elements are considered equal if and only if @@ -678,15 +734,38 @@ if (array_key_exists("first", $search_array)) { 100, "color" => "red"); -array_keys ($array); // returns array (0, "color") +print_r(array_keys ($array)); $array = array ("blue", "red", "green", "blue", "blue"); -array_keys ($array, "blue"); // returns array (0, 3, 4) +print_r(array_keys ($array, "blue")); $array = array ("color" => array("blue", "red", "green"), "size" => array("small", "medium", "large")); -array_keys ($array); // returns array ("color", "size") +print_r(array_keys ($array)); ]]> + + The printout of the program above will be: + + 0 + [1] => color +) +Array +( + [0] => 0 + [1] => 3 + [2] => 4 +) +Array +( + [0] => color + [1] => size +) +]]> + + @@ -758,14 +837,26 @@ function cube($n) { $a = array(1, 2, 3, 4, 5); $b = array_map("cube", $a); +print_r($b); ]]> + + This makes $b have: + + 1 + [1] => 8 + [2] => 27 + [3] => 64 + [4] => 125 +) +]]> + + - - This will result in $b containing - array (1, 8, 27, 64, 125); - <function>array_map</function> - using more arrays @@ -783,54 +874,58 @@ $a = array(1, 2, 3, 4, 5); $b = array("uno", "dos", "tres", "cuatro", "cinco"); $c = array_map("show_Spanish", $a, $b); - print_r($c); -// will output: -// Array -// ( -// [0] => The number 1 is called uno in Spanish -// [1] => The number 2 is called dos in Spanish -// [2] => The number 3 is called tres in Spanish -// [3] => The number 4 is called cuatro in Spanish -// [4] => The number 5 is called cinco in Spanish -// ) - $d = array_map("map_Spanish", $a , $b); - print_r($d); - -// will output: -// Array -// ( -// [0] => Array -// ( -// [1] => uno -// ) -// -// [1] => Array -// ( -// [2] => dos -// ) -// -// [2] => Array -// ( -// [3] => tres -// ) -// -// [3] => Array -// ( -// [4] => cuatro -// ) -// -// [4] => Array -// ( -// [5] => cinco -// ) -// -// ) ]]> + + This results: + + The number 1 is called uno in Spanish + [1] => The number 2 is called dos in Spanish + [2] => The number 3 is called tres in Spanish + [3] => The number 4 is called cuatro in Spanish + [4] => The number 5 is called cinco in Spanish +) + +// printout of $d +Array +( + [0] => Array + ( + [1] => uno + ) + + [1] => Array + ( + [2] => dos + ) + + [2] => Array + ( + [3] => tres + ) + + [3] => Array + ( + [4] => cuatro + ) + + [4] => Array + ( + [5] => cinco + ) + +) +]]> + + @@ -856,52 +951,57 @@ $c = array("uno", "dos", "tres", "cuatro", "cinco"); $d = array_map(null, $a, $b, $c); print_r($d); - -// will output: -// Array -// ( -// [0] => Array -// ( -// [0] => 1 -// [1] => one -// [2] => uno -// ) -// -// [1] => Array -// ( -// [0] => 2 -// [1] => two -// [2] => dos -// ) -// -// [2] => Array -// ( -// [0] => 3 -// [1] => three -// [2] => tres -// ) -// -// [3] => Array -// ( -// [0] => 4 -// [1] => four -// [2] => cuatro -// ) -// -// [4] => Array -// ( -// [0] => 5 -// [1] => five -// [2] => cinco -// ) -// -// ) ]]> + + The printout of the program above will be: + + Array + ( + [0] => 1 + [1] => one + [2] => uno + ) + + [1] => Array + ( + [0] => 2 + [1] => two + [2] => dos + ) + + [2] => Array + ( + [0] => 3 + [1] => three + [2] => tres + ) + + [3] => Array + ( + [0] => 4 + [1] => four + [2] => cuatro + ) + + [4] => Array + ( + [0] => 5 + [1] => five + [2] => cinco + ) + +) +]]> + + - See also array_filter, + See also array_filter and array_reduce. @@ -945,12 +1045,25 @@ $array2 = array ("a", "b", "color" => "green", "shape" => "trapezoid", 4); $result = array_merge ($array1, $array2); ]]> + + The $result will be: + + green + [0] => 2 + [1] => 4 + [2] => a + [3] => b + [shape] => trapezoid + [4] => 4 +) +]]> + + - - Resulting array will be array("color" => "green", 2, 4, - "a", "b", "shape" => "trapezoid", 4). - See also array_merge_recursive. @@ -997,12 +1110,31 @@ $ar2 = array (10, "color" => array ("favorite" => "green", "blue")); $result = array_merge_recursive ($ar1, $ar2); ]]> + + The $result will be: + + Array + ( + [favorite] => Array + ( + [0] => red + [1] => green + ) + + [0] => blue + ) + + [0] => 5 + [1] => 10 +) +]]> + + - - Resulting array will be array ("color" => array - ("favorite" => array ("red", "green"), "blue"), 5, 10). - See also array_merge. @@ -1198,17 +1330,27 @@ $result = array_pad ($input, 2, "noop"); <function>array_pop</function> example + + After this, $stack will have only 3 elements: + + orange + [1] => banana + [2] => apple +) +]]> + + and rasberry will be assigned to + $fruit. + - - After this, $stack has only 2 elements: - "orange" and "apple", and $fruit has - "raspberry". - &return.falseproblem; See also array_push, @@ -1258,18 +1400,29 @@ $array[] = $var; <function>array_push</function> example + + This example would result in $stack having + the following elements: + + orange + [1] => banana + [2] => apple + [3] => raspberry +) +]]> + + - This example would result in $stack having 4 - elements: 1, 2, "+", and 3. - - - See also: array_pop, + See also array_pop, array_shift, and array_unshift. @@ -1357,20 +1510,47 @@ print $input[$rand_keys[1]]."\n"; <function>array_reverse</function> example + + This makes both $result and + $result_keyed have the same elements, but + note the difference between the keys. The printout of + $result and + $result_keyed will be: + + Array + ( + [0] => green + [1] => red + ) + + [1] => 4 + [2] => php +) +Array +( + [2] => Array + ( + [0] => green + [1] => red + ) + + [1] => 4 + [0] => php +) +]]> + + + - - This makes both $result and - $result_keyed be array(array - ("green", "red"), 4.0, "php"). But - $result_keyed[0] is still - "php". - The second parameter was added in PHP 4.0.3. @@ -1438,7 +1618,7 @@ $d = array_reduce($x, "rsum", 1); $d containing 1. - See also array_filter, + See also array_filter, and array_map. @@ -1471,19 +1651,30 @@ $d = array_reduce($x, "rsum", 1); <function>array_shift</function> example + + This would result in $fruit having 3 elements left: + + banana + [1] => apple + [2] => raspberry +) +]]> + + and orange will be assigned to + $fruit. + - - This would result in $args having one element - "-f" left, and $opt being "-v". - See also array_unshift, - array_push, and + array_push and array_pop. @@ -1536,10 +1727,10 @@ $opt = array_shift ($args); @@ -1673,7 +1864,7 @@ array_splice ($input, -1, 1, array("black", "maroon")); mixed array_sum - array arr + array array @@ -1687,13 +1878,20 @@ array_splice ($input, -1, 1, array("black", "maroon")); 1.2,"b"=>2.3,"c"=>3.4); echo "sum(b) = ".array_sum($b)."\n"; -// prints: sum(b) = 6.9 ]]> + + The printout of the program above will be: + + + + @@ -1726,9 +1924,11 @@ echo "sum(b) = ".array_sum($b)."\n"; without duplicate values. - Note that keys are preserved. array_unique will - keep the first key encountered for every value, and ignore all - following keys. + Note that keys are preserved. array_unique sorts + the values treated as string at first, then will keep the first key + encountered for every value, and ignore all following keys. It does not + mean that the key of the first related value from the unsorted + array will be kept. @@ -1755,15 +1955,21 @@ echo "sum(b) = ".array_sum($b)."\n"; $input = array ("a" => "green", "red", "b" => "green", "blue", "red"); $result = array_unique ($input); print_r($result); -// this will output : -//Array -//( -// [a] => green -// [0] => red -// [1] => blue -//) ]]> + + This will output: + + green + [1] => blue + [2] => red +) +]]> + + @@ -1774,17 +1980,21 @@ print_r($result); $input = array (4,"4","3",4,3,"3"); $result = array_unique ($input); var_dump($result); - -/* output: -array(2) { - [0]=> - int(4) - [1]=> - string(1) "3" -} -*/ ]]> + + The printout of the program above will be (PHP 4.0.6): + + + int(4) + [4]=> + int(3) +} +]]> + + @@ -1826,19 +2036,30 @@ array(2) { <function>array_unshift</function> example + + This would result in $queue having the + following elements: + + apple + [1] => raspberry + [2] => orange + [3] => banana +) +]]> + + - - This would result in $queue having 5 - elements: "p4", "p5", "p6", "p1", and "p3". - See also array_shift, - array_push, and + array_push and array_pop. @@ -1867,9 +2088,21 @@ array_unshift ($queue, "p4", "p5", "p6"); "XL", "color" => "gold"); -array_values ($array); // returns array ("XL", "gold") +print_r(array_values ($array)); ]]> + + This will output: + + XL + [1] => gold +) +]]> + + @@ -1913,7 +2146,7 @@ function array_values ($arr) { int array_walk - array arr + array array string func mixed userdata @@ -1922,7 +2155,7 @@ function array_values ($arr) { Applies the user-defined function named by func - to each element of arr. + to each element of array. func will be passed array value as the first parameter and array key as the second parameter. If userdata is supplied, it will be passed as @@ -1980,14 +2213,32 @@ function test_alter (&$item1, $key, $prefix) { function test_print ($item2, $key) { echo "$key. $item2
\n"; } - +echo "Before ...:\n"; array_walk ($fruits, 'test_print'); reset ($fruits); array_walk ($fruits, 'test_alter', 'fruit'); +echo "... and after:\n"; reset ($fruits); array_walk ($fruits, 'test_print'); ]]> + + The printout of the program above will be: + + + + @@ -2054,8 +2305,8 @@ c = apple see sort. - See also: asort, rsort, - ksort, and sort. + See also asort, rsort, + ksort and sort. @@ -2117,7 +2368,7 @@ a = orange See also arsort, rsort, - ksort, and sort. + ksort and sort. @@ -2173,9 +2424,17 @@ $result = compact ("event", "nothing_here", $location_vars); ]]> - After this, $result will be array - ("event" => "SIGGRAPH", "city" => "San Francisco", - "state" => "CA"). + After this, $result will be: + + SIGGRAPH + [city] => San Francisco + [state] => CA +) +]]> + @@ -2244,8 +2503,8 @@ $result = count ($b); - The sizeof function is an alias for - count. + The sizeof function is an + alias for count. @@ -2293,8 +2552,8 @@ $result = count ($b); - See also: end, next, - prev, and reset. + See also end, next, + prev andreset. @@ -2398,7 +2657,7 @@ while (list ($key, $val) = each ($HTTP_POST_VARS)) { See also key, list, current, reset, - next, and prev. + next andprev. @@ -2423,9 +2682,9 @@ while (list ($key, $val) = each ($HTTP_POST_VARS)) { internal pointer to the last element, and returns that element. - See also: current, + See also current, each, - next, and reset. + next andreset. @@ -2588,7 +2847,7 @@ blue, large, sphere, medium will not produce results. - See also: compact. + See also compact. @@ -2636,11 +2895,20 @@ $os = array ("Mac", "NT", "Irix", "Linux"); if (in_array ("Irix", $os)) { print "Got Irix"; } -if (in_array ("mac", $os)) { # fails because in_array() is case-sensitive +if (in_array ("mac", $os)) { print "Got mac"; } ]]> + + The second condition fails because in_array() + is case-sensitive, so the program above will display: + + + + @@ -2656,12 +2924,16 @@ if (in_array('12.4', $a, TRUE)) if (in_array(1.13, $a, TRUE)) echo "1.13 found with strict check\n"; ?> - -// This will output: - -1.13 found with strict check ]]> + + This will display: + + + + @@ -2766,16 +3038,14 @@ while (list ($key, $val) = each ($fruits)) { This example would display: - - + - - + You may modify the behavior of the sort using the optional @@ -2784,8 +3054,8 @@ a = orange See also asort, arsort, - ksort sort, - natsortand rsort. + ksort, sort, + natsort and rsort. @@ -2827,16 +3097,14 @@ while (list ($key, $val) = each ($fruits)) { This example would display: - - + - - + You may modify the behavior of the sort using the optional @@ -2845,7 +3113,7 @@ d = lemon See also asort, arsort, - sort, natsort, and + sort, natsort and rsort. @@ -2952,8 +3220,7 @@ print_r($array2); The code above will generate the following output: - - + img12.png ) ]]> - - + For more information see: Martin Pool's Natural Order String Comparison page. @@ -3066,9 +3332,9 @@ Array - See also: + See also current, end, - prev, and reset. + prev andreset. @@ -3087,10 +3353,11 @@ Array - This is an alias for current. + This is an alias + for current. - See also: + See also end, next, prev and reset. @@ -3129,8 +3396,8 @@ Array pointer one place instead of advancing it. - See also: current, end, - next, and reset. + See also current, end, + next andreset. @@ -3225,7 +3492,7 @@ foreach(array_map('chr', range(ord('a'),ord('z'))) as $character) { element. - See also: current, + See also current, each, next, and prev. @@ -3268,16 +3535,14 @@ while (list ($key, $val) = each ($fruits)) { This example would display: - - + - - + The fruits have been sorted in reverse alphabetical order. @@ -3288,9 +3553,9 @@ while (list ($key, $val) = each ($fruits)) { see sort. - See also: arsort, + See also arsort, asort, ksort, - sort, and usort. + sort andusort. @@ -3348,8 +3613,8 @@ while (list (, $number) = each ($numbers)) { - The sizeof function is an alias for - count. + The sizeof function is an + alias for count. See also count. @@ -3398,16 +3663,14 @@ while (list ($key, $val) = each ($fruits)) { This example would display: - - + - - + The fruits have been sorted in alphabetical order. @@ -3431,7 +3694,7 @@ fruits[3] = orange - See also: arsort, + See also arsort, asort, ksort, natsort, natcasesort, rsort, usort, @@ -3479,7 +3742,7 @@ fruits[3] = orange ¬e.func-callback; - See also: usort, uksort, + See also usort, uksort, sort, asort, arsort, ksort and rsort. @@ -3532,20 +3795,18 @@ while (list ($key, $value) = each ($a)) { This example would display: - - + - - + ¬e.func-callback; - See also: usort, uasort, + See also usort, uasort, sort, asort, arsort, ksort, natsort and rsort. @@ -3582,10 +3843,6 @@ while (list ($key, $value) = each ($a)) { second. If two members compare as equal, their order in the sorted array is undefined. - - It is also possible to use a member function of an object as the - comparison function. See example number 3 below. - <function>usort</function> example @@ -3611,8 +3868,7 @@ while (list ($key, $value) = each ($a)) { This example would display: - - + - - + @@ -3661,17 +3916,15 @@ while (list ($key, $value) = each ($fruits)) { This example would display: - - + - - + - + ¬e.func-callback; @@ -3714,13 +3967,11 @@ foreach ($a as $item) { This example would display: </para> <para> - <informalexample> - <programlisting> + <screen> b c d - </programlisting> - </informalexample> + </screen> </para> <warning> <para> @@ -3730,11 +3981,11 @@ d </para> </warning> <para> - See also: <function>uasort</function>, + See also <function>uasort</function>, <function>uksort</function>, <function>sort</function>, <function>asort</function>, <function>arsort</function>,<function>ksort</function>, - <function>natsort</function>, and <function>rsort</function>. + <function>natsort</function> and<function>rsort</function>. </para> </refsect1> </refentry>