Re: array sort help...
| From: | Lars Torben Wilson | Date: | Wed, 11 Jun 2003 20:45:10 +0000 |
| Subject: | Re: array sort help... | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-151143@lists.php.net to get a copy of this message | ||
On Wed, 2003-06-11 at 12:10, Dan Joseph wrote:
> Hi,
>
> I cannot figure this out... Need some assistance.
>
> I have an array:
>
> $jack[#] = array(
> "loan_info" => 101,
> "first_name" => jack,
> "last_name" => mother
> );
>
> # = 0 thru 12
>
> I want to sort the array by loan_info. Could someone please explain this
> to me? I kind of understand array sorting, but I am still lost...
>
> -Dan Joseph
If you want to maintain key order, use uasort(); if, however, you want
the array to be reindexed sequentially when you sort it, use usort():
http://www.php.net/manual/en/function.uasort.php
http://www.php.net/manual/en/function.usort.php
A short example (with the usual caveats about 'make sure you add error
checking):
<?php
error_reporting(E_ALL);
$data =
array(array('loan_info' => 104,
'first_name' => 'jack',
'last_name' => 'mother'),
array('loan_info' => 102,
'first_name' => 'jill',
'last_name' => 'brother'),
array('loan_info' => 108,
'first_name' => 'jim',
'last_name' => 'the'),
array('loan_info' => 107,
'first_name' => 'bob',
'last_name' => 'whole'),
array('loan_info' => 105,
'first_name' => 'alice',
'last_name' => 'fam'),
array('loan_info' => 103,
'first_name' => 'ellen',
'last_name' => 'damily'));
function cmp($a, $b) {
return strcmp($a['loan_info'], $b['loan_info']);
}
uasort($data, 'cmp');
print_r($data);
?>
Hope this helps,
Torben
--
Torben Wilson <torben@php.net> +1.604.709.0506
http://www.thebuttlesschaps.com http://www.inflatableeye.com
http://www.hybrid17.com http://www.themainonmain.com
-----==== Boycott Starbucks! http://www.haidabuckscafe.com ====-----