RE: [PHP] sorting multi array
| From: | Chris Boget | Date: | Wed, 25 Apr 2007 10:39:22 +0000 |
| Subject: | RE: [PHP] sorting multi array | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-253682@lists.php.net to get a copy of this message | ||
> I have the following array, which I need to sort by quantity...
> I need to keep the indexes if poss.
This may not be the most elegant solution. Let's call your array
$origArray
$tmpArray = array();
foreach( $origArray as $elKey => $elArray ) {
$tmpArray[$elArray['quantity']] = $elKey;
}
if( ksort( $tmpArray )) {
$sortedArray = array();
foreach( $tmpArray as $elKey ) {
$sortedArray[$elKey] = $origArray[$elKey];
}
echo 'Original: <pre>' . print_r( $origArray, TRUE )
'</pre><br>';
echo 'Sorted: <pre>' . print_r( $sortedArray, TRUE ) '</pre>';
} else {
echo 'Sorry, couldn\'t sort';
}
thnx,
Chris