PHP/Data Structure/arsort
Содержание
arsort() function maintains the original index association and sorts the elements in reverse order.
<source lang="html4strict">
Its syntax is: void arsort (array array) <?
$cities = array ("A" => "a", "I" => "i", "A" => "z", "F" => "f", "C" => "c"); arsort($cities); for (reset ($cities); $key = key ($cities); next ($cities)) : print "cities[$key] = $cities[$key]
"; endfor;
?>
</source>
arsort( ) function reverse sorts it by its values while preserving the keys
<source lang="html4strict">
//bool arsort ( array &arr [, int options] ) <?
$capitalcities["England"] = "London"; $capitalcities["Wales"] = "Cardiff"; $capitalcities["Scotland"] = "Edinburgh"; arsort($capitalcities);
?>
</source>
Reversing an Array Using arsort()
<source lang="html4strict">
<?php
$dogs = array("A" => "C", "B" => "D", "X" => "Z", "Q" => "T"); arsort($dogs);printf("
%s\n", var_export($dogs, TRUE));
?>
</source>
Sorting with arsort()
<source lang="html4strict">
<? $meal = array("breakfast" => "A",
"lunch" => "B", "snack" => "C", "dinner" => "D");
print "Before Sorting:\n"; foreach ($meal as $key => $value) {
print " \$meal: $key $value\n";
} arsort($meal); print "After Sorting:\n"; foreach ($meal as $key => $value) {
print " \$meal: $key $value\n";
} ?>
</source>