RE: [PHP] array sort question
| From: | Ed Lazor | Date: | Sat, 02 Oct 2004 17:59:12 +0000 |
| Subject: | RE: [PHP] array sort question | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-198500@lists.php.net to get a copy of this message | ||
Thanks for the example using array_multisort. I'd been wondering how I
could use that function to do what I want. It looks like it's more flexible
in the long run, but I was able to use Paul's recommendation to do what I
want. Here's the code I ended up using:
function cmp ($a, $b) {
return strcmp($a["Title"], $b["Title"]);
}
uasort($list, "cmp");
-Ed
> -----Original Message-----
> Sorry for the empty reply (miss slide of the finger on my touch pad).
> You can use array_multisort(); The code would look something like
> this, remember, this is untested code so be warned before any flaming
> is done:
>
> //CODE
> //You need to restructure your array like this:
> $menu["ID"][1] = 5;
> $menu["Title"][1] = "Test 1";
> $menu["ID"][2] = 3;
> $menu["Title"][2] = "Test 4";
> //Then run array_multisort
> array_multisort($menu['Title'],$menu['ID']);
>
> //END CODE
>
> Like this it will sort alphabetically or numerically (which ever
> needed) ascending. You can put tags after the first variable like
> this:
>
> array_multisort($menu['Title'],SORT_ASC,SORT_STRING,$menu['ID']);
>
> If there are any more keys in the $menu array you need to list them
> after the $menu['ID'] key so they will be sorted according to the
> $menu['Title'] key. The first key you put into array_multisort() is
> the one that the rest get sorted by.