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

(singke) #1

<?
// build array
$users = array("bob"=>"Robert",
"steve"=>"Stephen",
"jon"=>"Jonathon");


// sort array
arsort($users);


// print out the values
for(reset($users); $index=key($users); next($users))
{
print("$index : $users[$index]
\n");
}
?>


asort(array unsorted_array)


The asort function sorts an array by its values. The indices are moved along with the
values. This sort is intended for associative arrays. Chapter 15 discusses sorting in depth.


<?
// build array
$users = array("bob"=>"Robert",
"steve"=>"Stephen",
"jon"=>"Jonathon");


// sort array
asort($users);


// print out the values
for(reset($users); $index=key($users); next($users))
{
print("$index : $users[$index]
\n");
}
?>


array compact(...)


The compact function returns an array containing the names and values of variables
named by the arguments. Any number of arguments may be passed, and they may be
single string values or arrays of string values. Arrays containing other arrays will be
recursively explored. The variables must be in the current scope. This function
complements extract, which creates variables from an array.


Figure 9-4. compact.
Free download pdf