note 19545 modified in function.asort by vrana
| From: | vrana@php.net | Date: | Tue, 17 Aug 2004 14:52:22 +0000 |
| Subject: | note 19545 modified in function.asort by vrana | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-79849@lists.php.net to get a copy of this message | ||
Here's my version of sorting multi-dimensional array by 2nd index.
Feel free to change the code to suit your needs.
<?php
function aSortBySecondIndex($multiArray, $secondIndex) {
while (list($firstIndex, ) = each($multiArray))
$indexMap[$firstIndex] = $multiArray[$firstIndex][$secondIndex];
asort($indexMap);
while (list($firstIndex, ) = each($indexMap))
if (is_numeric($firstIndex))
$sortedArray[] = $multiArray[$firstIndex];
else $sortedArray[$firstIndex] = $multiArray[$firstIndex];
return $sortedArray;
}
?>
--was--
Here's my version of sorting multi-dimensional array by 2nd index.
Feel free to change the code to suit your needs.
function aSortBySecondIndex($multiArray, $secondIndex) {
while (list($firstIndex, ) = each($multiArray))
$indexMap[$firstIndex] = $multiArray[$firstIndex][$secondIndex];
asort($indexMap);
while (list($firstIndex, ) = each($indexMap))
if (is_numeric($firstIndex))
$sortedArray[] = $multiArray[$firstIndex];
else $sortedArray[$firstIndex] = $multiArray[$firstIndex];
return $sortedArray;
}
http://php.net/manual/en/function.asort.php