PHP/Data Structure/arsort
Содержание
arsort() function maintains the original index association and sorts the elements in reverse order.
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] <br>";
endfor;
?>
arsort( ) function reverse sorts it by its values while preserving the keys
//bool arsort ( array &arr [, int options] )
<?
$capitalcities["England"] = "London";
$capitalcities["Wales"] = "Cardiff";
$capitalcities["Scotland"] = "Edinburgh";
arsort($capitalcities);
?>
Reversing an Array Using arsort()
<?php
$dogs = array("A" => "C", "B" => "D", "X" => "Z", "Q" => "T");
arsort($dogs);
printf("<pre>%s</pre>\n", var_export($dogs, TRUE));
?>
Sorting with arsort()
<?
$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";
}
?>