RE: [PHP] sorting multi array
| From: | Zoltán Németh | Date: | Wed, 25 Apr 2007 10:45:37 +0000 |
| Subject: | RE: [PHP] sorting multi array | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-253683@lists.php.net to get a copy of this message | ||
2007. 04. 25, szerda keltezéssel 11.39-kor Chris Boget ezt írta:
> > 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';
>
> }
this won't work if he has the same quantity for several keys, I think
greets
Zoltán Németh
>
> thnx,
> Chris
>