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

(singke) #1

value key(array arrayname)


The key function returns the index of the current element. Use current to find the value of
the current element.


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


for(reset($colors); $key = key($colors); next($colors))
{
print("$key is $colors[$key]
\n");
}
?>


boolean krsort(array data)


The krsort function sorts an array by its keys in reverse order—that is, largest values
first. The element values are moved along with the keys. This is mainly for the benefit of
associative arrays, since arrays indexed by integers can easily be traversed in order of
their keys.


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


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


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


boolean ksort(array data)


The ksort function sorts an array by its keys, or index values. The element values are
moved along with the keys. This is mainly for the benefit of associative arrays, since
arrays indexed by integers can easily be traversed in order of their keys.


<?

Free download pdf