Microsoft Word - Core PHP Programming Using PHP to Build Dynamic Web Sites

(singke) #1

array array_values(array data)


The array_values function returns just the array elements, re-indexed with integers.


<?
//set up an array of color names
$UserInfo = array("First Name"=>"Leon",
"Last Name"=>"Atkinson",
"Favorite Language"=>"PHP");


//re-index using integers
$UserInfo = array_values($UserInfo);


//print out all the values and their
//new indices
for($n=0; $n count($UserInfo); $n++)
{
print("($n) $UserInfo[$n]
\n");
}
?>


boolean array_walk(array data, string function)


The array_walk function executes the specified function on each element of the given
array. The function must take exactly one element; otherwise an error message is
generated. The array elements will be passed by reference, so any change made to them
by the specified function will be permanent in the array. The function specified must be
one you create, not a built-in PHP function.


<?
$colors = array("red", "blue", "green");


function printElement($element)
{
print("$element
\n");
}


array_walk($colors, "printElement");
?>


arsort(array unsorted_array)


The arsort function sorts an array in reverse order by its values. The indices are moved
along with the values. This sort is intended for associative arrays. Chapter 15, "Sorting,
Searching, and Random Numbers," discusses sorting in depth.

Free download pdf