diff --git a/functions/array.xml b/functions/array.xml index 0ae5be5ff7..6d86778e17 100644 --- a/functions/array.xml +++ b/functions/array.xml @@ -18,33 +18,32 @@ Returns an array of the parameters. The parameters can be given an index with the => operator. - - - - array is a language construct used to - represent literal arrays, and not a regular function. - - - + + + array is a language construct used to + represent literal arrays, and not a regular function. + + + The following example demonstrates how to create a two-dimensional array, how to specify keys for associative arrays, and how to skip-and-continue numeric indices in normal arrays. - <function>array</function> example - -$fruits = array( - "fruits" => array("a"=>"orange","b"=>"banana","c"=>"apple"), + +$fruits = array ( + "fruits" => array("a"=>"orange", "b"=>"banana", "c"=>"apple"), "numbers" => array(1, 2, 3, 4, 5, 6), "holes" => array("first", 5 => "second", "third") ); - - + + + - See also: - list. + See also: list. + @@ -59,25 +58,25 @@ $fruits = array( array array_count_values array input - - array_count_values returns an array using the values - of the input array as keys and their frequency in - input as values. - + array_count_values returns an array using + the values of the input array as keys and + their frequency in input as values. + <function>array_count_values</function> example - -$array = array(1,"hello",1,"world","hello"); -array_count_values($array); // returns array(1=>2, "hello"=>2, "world"=>1) + +$array = array(1, "hello", 1, "world", "hello"); +array_count_values($array); // returns array(1=>2, "hello"=>2, "world"=>1) - - - - - This function was added in PHP 4.0. - + + + + This function was added in PHP 4.0. + + + @@ -92,7 +91,6 @@ array_count_values($array); // returns array(1=>2, "hello"=>2, "world"=>1) array array_flip array trans - array_flip returns an array in flip order. @@ -104,13 +102,12 @@ $trans = array_flip ($trans); $original = strtr ($str, $trans); - This function was added in PHP 4.0. - - + + @@ -125,37 +122,40 @@ $original = strtr ($str, $trans); array array_keys array input mixed - search_value + + search_value + + - array_keys returns the keys, numeric and - string, from the input array. - + string, from the input array. + If the optional search_value is specified, then only the keys for that value are returned. Otherwise, all - the keys from the input are returned. - + the keys from the input are returned. + <function>array_keys</function> example - + $array = array(0 => 100, "color" => "red"); -array_keys($array); // returns array(0, "color") +array_keys ($array); // returns array (0, "color") $array = array(1, 100, 2, 100); -array_keys($array, 100); // returns array(0, 2) +array_keys ($array, 100); // returns array (0, 2) - - + + See also array_values. - - - - This function was added in PHP 4.0. - + + + This function was added in PHP 4.0. + + + @@ -174,42 +174,45 @@ array_keys($array, 100); // returns array(0, 2) ... - array_merge merges the elements of two or more arrays together so that the values of one are appended to - the end of the previous one. It returns the resulting array. - + the end of the previous one. It returns the resulting array. + If the input arrays had the same string keys, then the later value for that key will overwrite previous one. If, however, the arrays have the same numeric key, this does not happen since the - values are appended. - + values are appended. + <function>array_merge</function> example - -$array1 = array("color" => "red", 2, 4); -$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid"); -array_merge($array1, $array2); + +$array1 = array ("color" => "red", 2, 4); +$array2 = array ("a", "b", "color" => "green", "shape" => "trapezoid"); +array_merge ($array1, $array2); Resulting array will be array("color" => "green", 2, 4, "a", - "b", "shape" => "trapezoid"). + "b", "shape" => "trapezoid"). + - - - - This function was added in PHP 4.0. - + + + This function was added in PHP 4.0. + + + array_pad - Pad array to the specified length with a value + + Pad array to the specified length with a value + Description @@ -219,7 +222,6 @@ array_merge($array1, $array2); int pad_size mixed pad_value - array_pad returns a copy of the input padded to size specified by @@ -231,20 +233,19 @@ array_merge($array1, $array2); equal to the length of the input then no padding takes place. - <function>array_pad</function> example - -$input = array(12, 10, 9); + +$input = array (12, 10, 9); -$result = array_pad($input, 5, 0); -// result is array(12, 10, 9, 0, 0) +$result = array_pad ($input, 5, 0); +// result is array (12, 10, 9, 0, 0) -$result = array_pad($input, -7, -1); -// result is array(-1, -1, -1, -1, 12, 10, 9) +$result = array_pad ($input, -7, -1); +// result is array (-1, -1, -1, -1, 12, 10, 9) -$result = array_pad($input, 2, "noop"); +$result = array_pad ($input, 2, "noop"); // not padded @@ -263,33 +264,34 @@ $result = array_pad($input, 2, "noop"); mixed array_pop array array - array_pop pops and returns the last value of the array, shortening the - array by one element. - + array by one element. + <function>array_pop</function> example - -$stack = array("orange", "apple", "raspberry"); -$fruit = array_pop($stack); + +$stack = array ("orange", "apple", "raspberry"); +$fruit = array_pop ($stack); After this, $stack has only 2 elements: "orange" and "apple", - and $fruit has "raspberry". - - + and $fruit has "raspberry". + + + See also array_push, array_shift, and array_unshift. - - - - This function was added in PHP 4.0. - + + + This function was added in PHP 4.0. + + + @@ -306,7 +308,9 @@ $fruit = array_pop($stack); int array_push array array mixed var - ... + + ... + @@ -315,42 +319,44 @@ $fruit = array_pop($stack); variables onto the end of array. The length of array increases by the number of variables pushed. Has the same effect as: - + $array[] = $var; - repeated for each var. - + repeated for each var. + - Returns the new number of elements in the array. - + Returns the new number of elements in the array. + <function>array_push</function> example - -$stack = array(1, 2); + +$stack = array (1, 2); array_push($stack, "+", 3); This example would result in $stack having 4 elements: 1, 2, "+", - and 3. - + and 3. + - See also: - array_pop, + See also: array_pop, array_shift, and array_unshift. - - - - This function was added in PHP 4.0. - + + + This function was added in PHP 4.0. + + + array_reverse - Return an array with elements in reverse order + + Return an array with elements in reverse order + Description @@ -358,31 +364,35 @@ array_push($stack, "+", 3); array array_reverse array array - - array_reverse takes input array and returns a new array with the order of the elements reversed. - + array_reverse takes input + array and returns a new array with the + order of the elements reversed. + <function>array_reverse</function> example - -$input = array("php", 4.0, array("green", "red")); -$result = array_reverse($input); + +$input = array ("php", 4.0, array ("green", "red")); +$result = array_reverse ($input); - This makes $result have array(array("green", "red"), 4.0, "php"). - + This makes $result have array (array ("green", "red"), 4.0, "php"). - This function was added in PHP 4.0 Beta 3. - + This function was added in PHP 4.0 Beta 3. + + + array_shift - Pop an element off the beginning of array + + Pop an element off the beginning of array + Description @@ -390,33 +400,33 @@ $result = array_reverse($input); mixed array_shift array array - array_shift shifts the first value of the array off and returns it, shortening the array by one element and moving everything - down. - + down. + <function>array_shift</function> example - -$args = array("-v", "-f"); -$opt = array_shift($args); + +$args = array ("-v", "-f"); +$opt = array_shift ($args); This would result in $args having one element "-f" left, and $opt - being "-v". - + being "-v". + See also array_unshift, array_push, and array_pop. - - - - This function was added in PHP 4.0. - + + + This function was added in PHP 4.0. + + + @@ -437,19 +447,18 @@ $opt = array_shift($args); - array_slice returns a sequence of elements from the array specified by the offset and length - parameters. - + parameters. + If offset is positive, the sequence will start at that offset in the array. If offset is negative, the sequence will - start that far from the end of the array. - + start that far from the end of the array. + If length is given and is positive, then the sequence will have that many elements in it. If @@ -457,28 +466,29 @@ $opt = array_shift($args); sequence will stop that many elements from the end of the array. If it is omitted, then the sequence will have everything from offset up until the end of the - array. - + array. + <function>array_slice</function> examples - -$input = array("a", "b", "c", "d", "e"); + +$input = array ("a", "b", "c", "d", "e"); -$output = array_slice($input, 2); // returns "c", "d", and "e" -$output = array_slice($input, 2, -1); // returns "c", "d" -$output = array_slice($input, -2, 1); // returns "d" -$output = array_slice($input, 0, 3); // returns "a", "b", and "c" +$output = array_slice ($input, 2); // returns "c", "d", and "e" +$output = array_slice ($input, 2, -1); // returns "c", "d" +$output = array_slice ($input, -2, 1); // returns "d" +$output = array_slice ($input, 0, 3); // returns "a", "b", and "c" - - + + See also array_splice. - - - - This function was added in PHP 4.0. - + + + This function was added in PHP 4.0. + + + @@ -500,24 +510,26 @@ $output = array_slice($input, 0, 3); // returns "a", "b", and "c" length array - replacement + + replacement + + - array_splice removed the elements designated by offset and length from the input array, and replaces them with the elements of the replacement array, if - supplied. - + supplied. + If offset is positive then the start of removed portion is at that offset from the beginning of the input array. If offset is negative then it starts that far - from the end of the input array. - + from the end of the input array. + If length is omitted, removes everything from offset to the end of the array. If @@ -529,8 +541,8 @@ $output = array_slice($input, 0, 3); // returns "a", "b", and "c" offset to the end of the array when replacement is also specified, use count($input) for - length. - + length. + If replacement array is specified, then the removed elements are replaced with elements from this array. @@ -540,25 +552,25 @@ $output = array_slice($input, 0, 3); // returns "a", "b", and "c" array are inserted in the place specified by the offset. Tip: if the replacement is just one element it is not necessary to put array() - around it, unless the element is an array itself. - + around it, unless the element is an array itself. + The following equivalences hold: - + array_push($input, $x, $y) array_splice($input, count($input), 0, array($x, $y)) array_pop($input) array_splice($input, -1) array_shift($input) array_splice($input, 0, 1) array_unshift($input, $x, $y) array_splice($input, 0, 0, array($x, $y)) $a[$x] = $y array_splice($input, $x, 1, $y) - - + + - Returns the array consisting of removed elements. - + Returns the array consisting of removed elements. + <function>array_splice</function> examples - + $input = array("red", "green", "blue", "yellow"); array_splice($input, 2); // $input is now array("red", "green") @@ -569,15 +581,16 @@ array_splice($input, -1, 1, array("black", "maroon")); // $input is now array("red", "green", // "blue", "black", "maroon") - - + + See also array_slice. - - - - This function was added in PHP 4.0. - + + + This function was added in PHP 4.0. + + + @@ -594,40 +607,44 @@ array_splice($input, -1, 1, array("black", "maroon")); int array_unshift array array mixed var - ... + + + ... + + - array_unshift prepends passed elements to the front of the array. Note that the list of elements is prepended as a whole, so that the prepended - elements stay in the same order. - + elements stay in the same order. + Returns the new number of elements in the - array. - + array. + <function>array_unshift</function> example - + $queue = array("p1", "p3"); array_unshift($queue, "p4", "p5", "p6"); This would result in $queue having 5 elements: "p4", "p5", "p6", - "p1", and "p3". - + "p1", and "p3". + See also array_shift, array_push, and array_pop. - - - - This function was added in PHP 4.0. - + + + This function was added in PHP 4.0. + + + @@ -642,24 +659,24 @@ array_unshift($queue, "p4", "p5", "p6"); array array_values array input - array_values returns all the values from the - input array. - + input array. + <function>array_values</function> example - + $array = array("size" => "XL", "color" => "gold"); array_values($array); // returns array("XL", "gold") - - This function was added in PHP 4.0. - + This function was added in PHP 4.0. + + + @@ -678,24 +695,23 @@ array_values($array); // returns array("XL", "gold") string func mixed userdata - Applies the function named by func to each - element of - arr. 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 the third parameter to the user - function. - + element of arr. + 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 + the third parameter to the user function. + + If func requires more than two or three arguments, depending on userdata, a warning will be generated each time array_walk calls func. These warnings may be suppressed by prepending the '@' sign to the array_walk - call, or by using error_reporting. - + call, or by using error_reporting. + If func needs to be working with the @@ -705,42 +721,42 @@ array_values($array); // returns array("XL", "gold") itself. - Passing the key and userdata to func was - added in 4.0. + added in 4.0. + In PHP 4 reset needs to be called as necessary since array_walk does not reset - the array by default. + the array by default. + - <function>array_walk</function> example - -$fruits = array("d"=>"lemon","a"=>"orange","b"=>"banana","c"=>"apple"); + +$fruits = array ("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple"); -function test_alter( &$item1, $key, $prefix ) { +function test_alter (&$item1, $key, $prefix) { $item1 = "$prefix: $item1"; } -function test_print( $item2, $key ) { +function test_print ($item2, $key) { echo "$key. $item2<br>\n"; } -array_walk( $fruits, 'test_print' ); -reset($fruits); -array_walk( $fruits, 'test_alter', 'fruit' ); -reset($fruits); -array_walk( $fruits, 'test_print' ); +array_walk ($fruits, 'test_print'); +reset ($fruits); +array_walk ($fruits, 'test_alter', 'fruit'); +reset ($fruits); +array_walk ($fruits, 'test_print'); - - + + - See also each and list. - + See also each and list. + @@ -764,25 +780,28 @@ array_walk( $fruits, 'test_print' ); the actual element order is significant. <function>arsort</function> example - -$fruits = array("d"=>"lemon","a"=>"orange","b"=>"banana","c"=>"apple"); -arsort($fruits); -for(reset($fruits); $key = key($fruits); next($fruits)) { + +$fruits = array ("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple"); +arsort ($fruits); +for (reset ($fruits); $key = key ($fruits); next ($fruits)) { echo "fruits[$key] = ".$fruits[$key]."\n"; } - + + This example would display: fruits[a] = orange fruits[d] = lemon fruits[b] = banana fruits[c] = apple - + The fruits have been sorted in reverse alphabetical order, and - the index associated with each element has been maintained. + the index associated with each element has been maintained. + See also: asort, rsort, - ksort, and sort. + ksort, and sort. + @@ -804,25 +823,28 @@ fruits[c] = apple the actual element order is significant. <function>asort</function> example - -$fruits = array("d"=>"lemon","a"=>"orange","b"=>"banana","c"=>"apple"); -asort($fruits); -for(reset($fruits); $key = key($fruits); next($fruits)) { + +$fruits = array ("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple"); +asort ($fruits); +for (reset ($fruits); $key = key ($fruits); next ($fruits)) { echo "fruits[$key] = ".$fruits[$key]."\n"; } - + + This example would display: fruits[c] = apple fruits[b] = banana fruits[d] = lemon fruits[a] = orange - + The fruits have been sorted in alphabetical order, and the index - associated with each element has been maintained. + associated with each element has been maintained. + See also arsort, rsort, - ksort, and sort. + ksort, and sort. + @@ -839,7 +861,9 @@ fruits[a] = orange array compact string varname | array varnames - ... + + ... + @@ -847,41 +871,42 @@ fruits[a] = orange parameters. Each parameter can be either a string containing the name of the variable, or an array of variable names. The array can contain other arrays of variable names inside it; - compact handles it recursively. - + compact handles it recursively. + For each of these, compact looks for a variable with that name in the current symbol table and adds it to the output array such that the variable name becomes the key and the contents of the variable become the value for that key. In short, it does the opposite of extract. - It returns the output array with all the variables added to it. - + It returns the output array with all the variables added to it. + <function>compact</function> example - + $city = "San Francisco"; $state = "CA"; $event = "SIGGRAPH"; -$location_vars = array("city", "state"); +$location_vars = array ("city", "state"); -$result = compact("event", $location_vars); +$result = compact ("event", $location_vars); - - After this, $result will be array("event" => "SIGGRAPH", "city" - => "San Francisco", "state" => "CA"). - - + After this, $result will be array ("event" => "SIGGRAPH", + "city" => "San Francisco", "state" => "CA"). + + + See also extract. - - - - This function was added in PHP 4.0. - + + + This function was added in PHP 4.0. + + + @@ -899,23 +924,27 @@ $result = compact("event", $location_vars); Returns the number of elements in var, which is typically an array (since anything else will have one - element). + element). + - Returns 1 if the variable is not an array. + Returns 1 if the variable is not an array. + Returns 0 if the variable is not set. - - - count may return 0 for a variable that - isn't set, but it may also return 0 for a variable that has been - initialized with an empty array. Use isset - to test if a variable is set. - - + + + count may return 0 for a variable that + isn't set, but it may also return 0 for a variable that has + been initialized with an empty array. Use + isset to test if a variable is set. + + + - See also: - sizeof, isset, and - is_array. + See also: sizeof, + isset, and + is_array. + @@ -933,8 +962,8 @@ $result = compact("event", $location_vars); Every array has an internal pointer to its "current" element, which is initialized to the first element inserted into the - array. - + array. + The current function simply returns the array element that's currently being pointed by the internal @@ -942,24 +971,25 @@ $result = compact("event", $location_vars); internal pointer points beyond the end of the elements list, current returns false. - - If the array contains empty elements (0 or "", the empty - string) then this function will return false for these elements - as well. This makes it impossible to determine if you are really - at the end of the list in such an array using - current. To properly traverse an array that - may contain empty elements, use the each - function. - - + + + If the array contains empty elements (0 or "", the empty + string) then this function will return false for these elements + as well. This makes it impossible to determine if you are + really at the end of the list in such an array using + current. To properly traverse an array + that may contain empty elements, use the + each function. + + + - See also: - end, next, - prev and reset. + See also: end, next, + prev and reset. + - each @@ -982,24 +1012,22 @@ $result = compact("event", $location_vars); value. Elements 0 and key contain the key name of the array element, and 1 and - value contain the data. - + value contain the data. + If the internal pointer for the array points past the end of the - array contents, each returns false. - + array contents, each returns false. + <function>each</function> examples - + $foo = array ("bob", "fred", "jussi", "jouni", "egon", "marliese"); -$bar = each( $foo ); +$bar = each ($foo); - $bar now contains the following key/value pairs: - 0 => 0 1 => 'bob' @@ -1007,56 +1035,55 @@ $bar = each( $foo ); value => 'bob' - -$foo = array( "Robert" => "Bob", "Seppo" => "Sepi" ); -$bar = each( $foo ); - - + +$foo = array ("Robert" => "Bob", "Seppo" => "Sepi"); +$bar = each ($foo); + + $bar now contains the following key/value pairs: - 0 => 'Robert' 1 => 'Bob' key => 'Robert' value => 'Bob' - - - + + + + each is typically used in conjunction with list to traverse an array; for instance, $HTTP_POST_VARS: - Traversing $HTTP_POST_VARS with <function>each</function> - + echo "Values submitted via POST method:<br>"; -reset($HTTP_POST_VARS); -while (list($key, $val) = each($HTTP_POST_VARS)) { - echo "$key => $val<br>"; +reset ($HTTP_POST_VARS); +while (list ($key, $val) = each ($HTTP_POST_VARS)) { + echo "$key => $val<br>"; } - - - - After each has executed, the array cursor - will be left on the next element of the array, or on the last - element if it hits the end of the array. + + + + After each has executed, the array cursor + will be left on the next element of the array, or on the last + element if it hits the end of the array. + See also key, list, - current, reset, - next, and prev. - + current, reset, + next, and prev. + - end @@ -1072,14 +1099,13 @@ while (list($key, $val) = each($HTTP_POST_VARS)) { end advances array's - internal pointer to the last element. + internal pointer to the last element. + - See also: - current, - each, - end, - next, and - reset. + See also: current, + each, end, + next, and reset. + @@ -1109,7 +1135,8 @@ while (list($key, $val) = each($HTTP_POST_VARS)) { names and values as variable values. For each key/value pair it will create a variable in the current symbol table, subject to extract_type and - prefix parameters. + prefix parameters. + extract checks for colissions with existing variables. The way collisions are treated is determined by @@ -1128,7 +1155,8 @@ while (list($key, $val) = each($HTTP_POST_VARS)) { EXTR_SKIP - If there is a collision, don't overwrite the existing variable. + If there is a collision, don't overwrite the existing + variable. @@ -1136,7 +1164,7 @@ while (list($key, $val) = each($HTTP_POST_VARS)) { EXTR_PREFIX_SAME If there is a collision, prefix the new variable with - prefix. + prefix. @@ -1148,55 +1176,62 @@ while (list($key, $val) = each($HTTP_POST_VARS)) { - + + If extract_type is not specified, it is - assumed to be EXTR_OVERWRITE. + assumed to be EXTR_OVERWRITE. + Note that prefix is only required if extract_type is EXTR_PREFIX_SAME or - EXTR_PREFIX_ALL. + EXTR_PREFIX_ALL. + extract checks each key to see if it constitues a valid variable name, and if it does only then does - it proceed to import it. + it proceed to import it. + A possible use for extract is to import into symbol table variables contained in an associative array returned by - wddx_deserialize. + wddx_deserialize. + - extract example - -<? + <function>Extract</function> example + +<php? /* Suppose that $var_array is an array returned from wddx_deserialize */ + $size = "large"; -$var_array = array("color" => "blue", - "size" => "medium", - "shape" => "sphere"); -extract($var_array, EXTR_PREFIX_SAME, "wddx"); +$var_array = array ("color" => "blue", + "size" => "medium", + "shape" => "sphere"); +extract ($var_array, EXTR_PREFIX_SAME, "wddx"); print "$color, $size, $shape, $wddx_size\n"; ?> - - - - The above example will produce: - + + + + The above example will produce: + blue, large, sphere, medium - - + + The $size wasn't overwritten, becaus we specified EXTR_PREFIX_SAME, which resulted in $wddx_size being created. If EXTR_SKIP was specified, then $wddx_size wouldn't even have been created. EXTR_OVERWRITE would have cause $size to have value "medium", and EXTR_PREFIX_ALL would result in new - variables being named $wddx_color, $wddx_size, and $wddx_shape. + variables being named $wddx_color, $wddx_size, and $wddx_shape. + @@ -1212,26 +1247,26 @@ blue, large, sphere, medium mixed needle array haystack - Searches haystack for needle and returns true if it is found in - the array, false otherwise. - + the array, false otherwise. + <function>in_array</function> example - -$os = array("Mac", "NT", "Irix", "Linux"); -if (in_array("Irix", $os)) - print "Got Irix"; + +$os = array ("Mac", "NT", "Irix", "Linux"); +if (in_array ("Irix", $os)) + print "Got Irix"; - - - - This function was added in PHP 4.0. - + + + This function was added in PHP 4.0. + + + @@ -1248,11 +1283,11 @@ if (in_array("Irix", $os)) key returns the index element of the - current array position. + current array position. + - See also: - current, - next + See also: current, next + @@ -1267,37 +1302,35 @@ if (in_array("Irix", $os)) int krsort array array - - Sorts an array by key in reverse order, maintaining key to data correlations. This - is useful mainly for associative arrays. - - + Sorts an array by key in reverse order, maintaining key to data + correlations. This is useful mainly for associative arrays. + <function>krsort</function> example - -$fruits = array("d"=>"lemon","a"=>"orange","b"=>"banana","c"=>"apple"); -krsort($fruits); -for(reset($fruits); $key = key($fruits); next($fruits)) { + +$fruits = array ("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple"); +krsort ($fruits); +for (reset ($fruits); $key = key ($fruits); next ($fruits)) { echo "fruits[$key] = ".$fruits[$key]."\n"; } - + + This example would display: fruits[d] = lemon fruits[c] = apple fruits[b] = banana fruits[a] = orange - - + + - See also asort, arsort, ksort - sort, and rsort. - + See also asort, arsort, + ksort sort, and + rsort. + - - ksort @@ -1309,36 +1342,34 @@ fruits[a] = orange int ksort array array - Sorts an array by key, maintaining key to data correlations. This is useful mainly for associative arrays. - - + <function>ksort</function> example - -$fruits = array("d"=>"lemon","a"=>"orange","b"=>"banana","c"=>"apple"); -ksort($fruits); -for(reset($fruits); $key = key($fruits); next($fruits)) { + +$fruits = array ("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple"); +ksort ($fruits); +for (reset ($fruits); $key = key ($fruits); next ($fruits)) { echo "fruits[$key] = ".$fruits[$key]."\n"; } - + + This example would display: fruits[a] = orange fruits[b] = banana fruits[c] = apple fruits[d] = lemon - - + + See also asort, arsort, - sort, and rsort. - + sort, and rsort. + - list @@ -1356,15 +1387,15 @@ fruits[d] = lemon Like array, this is not really a function, but a language construct. list is used to assign a list of variables in one operation. - <function>list</function> example - + <table> <tr> <th>Employee name</th> <th>Salary</th> </tr> + <?php $result = mysql($conn, "SELECT id, name, salary FROM employees"); @@ -1375,11 +1406,15 @@ while (list($id, $name, $salary) = mysql_fetch_row($result)) { " </tr>\n"); } -?></table> - +?> +</table> + + + - See also: each, array. + See also: each, array. + @@ -1398,7 +1433,8 @@ while (list($id, $name, $salary) = mysql_fetch_row($result)) { Returns the array element in the next place that's pointed by the - internal array pointer, or false if there are no more elements. + internal array pointer, or false if there are no more elements. + next behaves like current, with one difference. It advances @@ -1414,12 +1450,13 @@ while (list($id, $name, $salary) = mysql_fetch_row($result)) { an array which may contain empty elements see the each function. - - + + See also: current, end - prev and reset + prev and reset + @@ -1435,11 +1472,13 @@ while (list($id, $name, $salary) = mysql_fetch_row($result)) { array array - This is an alias for current. + This is an alias for current. + See also: end, next, - prev and reset. + prev and reset. + @@ -1458,25 +1497,24 @@ while (list($id, $name, $salary) = mysql_fetch_row($result)) { Returns the array element in the previous place that's pointed by the internal array pointer, or false if there are no more elements. - - - - If the array contains empty elements then this function will - return false for these elements as well. To properly traverse - an array which may contain empty elements see the - each function. - - - + + + If the array contains empty elements then this function will + return false for these elements as well. To properly traverse + an array which may contain empty elements see the + each function. + + + prev behaves just like next, except it rewinds the internal array - pointer one place instead of advancing it. - + pointer one place instead of advancing it. + - See also: - current, end - next and reset + See also: current, end + next and reset + @@ -1497,9 +1535,11 @@ while (list($id, $name, $salary) = mysql_fetch_row($result)) { range returns an array of integers from low to high, - inclusive. + inclusive. + - See shuffle for an example of its use. + See shuffle for an example of its use. + @@ -1518,17 +1558,17 @@ while (list($id, $name, $salary) = mysql_fetch_row($result)) { reset rewinds array's - internal pointer to the first element. + internal pointer to the first element. + reset returns the value of the first array - element. + element. + - See also: - current, - each, - next, - prev, and - reset. + See also: current, + each, next, + prev, and reset. + @@ -1547,33 +1587,28 @@ while (list($id, $name, $salary) = mysql_fetch_row($result)) { This function sorts an array in reverse order (highest to lowest). <function>rsort</function> example - -$fruits = array("lemon","orange","banana","apple"); -rsort($fruits); -for (reset($fruits); list($key,$value) = each($fruits); ) { + +$fruits = array ("lemon", "orange", "banana", "apple"); +rsort ($fruits); +for (reset ($fruits); list ($key, $value) = each ($fruits); ) { echo "fruits[$key] = ", $value, "\n"; } - This example would display: - fruits[0] = orange fruits[1] = lemon fruits[2] = banana fruits[3] = apple - - The fruits have been sorted in reverse alphabetical order. - + The fruits have been sorted in reverse alphabetical order. + - See also: - arsort, - asort, - ksort, - sort, and - usort. + See also: arsort, + asort, ksort, + sort, and usort. + @@ -1593,18 +1628,21 @@ fruits[3] = apple an array. <function>shuffle</function> example - -$numbers = range(1,20); -srand(time()); -shuffle($numbers); -while (list(,$number) = each($numbers)) { + +$numbers = range (1,20); +srand (time()); +shuffle ($numbers); +while (list(, $number) = each ($numbers)) { echo "$number "; } - + + + See also arsort, asort, ksort, rsort, - sort and usort. + sort and usort. + @@ -1620,10 +1658,11 @@ while (list(,$number) = each($numbers)) { array array - Returns the number of elements in the array. + Returns the number of elements in the array. + - See also: - count + See also: count + @@ -1643,28 +1682,28 @@ while (list(,$number) = each($numbers)) { lowest to highest when this function has completed. <function>sort</function> example - -$fruits = array("lemon","orange","banana","apple"); -sort($fruits); -for(reset($fruits); $key = key($fruits); next($fruits)) { + +$fruits = array ("lemon", "orange", "banana", "apple"); +sort ($fruits); +for (reset ($fruits); $key = key ($fruits); next ($fruits)) { echo "fruits[$key] = ".$fruits[$key]."\n"; } - + + This example would display: fruits[0] = apple fruits[1] = banana fruits[2] = lemon fruits[3] = orange - - The fruits have been sorted in alphabetical order. + + The fruits have been sorted in alphabetical order. + - See also: - arsort, - asort, - ksort, - rsort, and - usort. + See also: arsort, + asort, ksort, + rsort, and usort. + @@ -1688,7 +1727,8 @@ fruits[3] = orange their correlation with the array elements they are associated with. This is used mainly when sorting associative arrays where the actual element order is significant. The comparison function - is user-defined. + is user-defined. + @@ -1714,36 +1754,32 @@ fruits[3] = orange <function>uksort</function> example - -function mycompare($a, $b) { + +function mycompare ($a, $b) { if ($a == $b) return 0; return ($a > $b) ? -1 : 1; } -$a = array(4 => "four", 3 => "three", 20 => "twenty", 10 => "ten"); -uksort($a, mycompare); -while(list($key, $value) = each($a)) { +$a = array (4 => "four", 3 => "three", 20 => "twenty", 10 => "ten"); +uksort ($a, mycompare); +while (list ($key, $value) = each ($a)) { echo "$key: $value\n"; } - - + + This example would display: - 20: twenty 10: ten 4: four 3: three - - + + - See also: - arsort, - asort, - uasort, - ksort, - rsort, and - sort. - + See also: arsort, + asort, uasort, + ksort, rsort, and + sort. + @@ -1765,7 +1801,8 @@ while(list($key, $value) = each($a)) { This function will sort an array by its values using a user-supplied comparison function. If the array you wish to sort needs to be sorted by some non-trivial criteria, you should use - this function. + this function. + The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to @@ -1774,17 +1811,18 @@ while(list($key, $value) = each($a)) { sorted array is undefined. <function>usort</function> example - -function cmp($a,$b) { + +function cmp ($a, $b) { if ($a == $b) return 0; return ($a > $b) ? -1 : 1; } -$a = array(3,2,5,6,1); -usort($a, cmp); -while(list($key,$value) = each($a)) { +$a = array (3, 2, 5, 6, 1); +usort ($a, cmp); +while (list ($key, $value) = each ($a)) { echo "$key: $value\n"; } - + + This example would display: 0: 6 @@ -1792,29 +1830,26 @@ while(list($key,$value) = each($a)) { 2: 3 3: 2 4: 1 - - - - - Obviously in this trivial case the rsort - function would be more appropriate. - - - - - - The underlying quicksort function in some C libraries (such as on - Solaris systems) may cause PHP to crash if the comparison function - does not return consistent values. - - - + + + + Obviously in this trivial case the rsort + function would be more appropriate. + + + + + The underlying quicksort function in some C libraries (such as + on Solaris systems) may cause PHP to crash if the comparison + function does not return consistent values. + + + - See also: - arsort, asort, - ksort, rsort and - sort. - + See also: arsort, + asort, ksort, + rsort and sort. +