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

(singke) #1

$colors = array("red"=>"FF0000",
"green"=>"00FF00",
"blue"=>"0000FF");


// sort an array by its keys
ksort($colors);


// print out the values
foreach($colors as $key=>$value)
{
print("$key : $value
\n");
}
?>


list(...)


The list function treats a list of variables as if they were an array. It may only be used
on the left side of an assignment operator. It is useful for translating a returned array
directly into a set of variables.


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


//put first two elements of returned array
//into key and value, respectively
list($key, $value) = each($colors);


print("$key: $value
\n");
?>


value max(array arrayname) value max(...)


The max function returns the largest value from all the array elements. If all values are
strings, then the values will be compared as strings. If any of the values is a number, only
the integers and doubles will be compared numerically. The alternate version of the max
function takes any number of arguments and returns the largest of them. With this use,
you must supply at least two values.


To find the minimum value, use min.


<?
$colors = array("red"=>"FF0000",
"green"=>"00FF00",
"blue"=>"0000FF");


//prints FF0000
print(max($colors). "
\n");

Free download pdf