Weirdan is correct in saying that array_multisort will work for your current problem. It's not working for you, right now, because your array is mal-formed. Here's what I came up with for you:
Code: Select all
<?php
function my_sort($a,$b)
{
if ($a[0] == $b[0]) {
return 0;
}
return ($a[0] < $b[0]) ? -1 : 1;
}
function my_sort_string($a,$b)
{
if ($a[1] == $b[1]) {
return 0;
}
return ($a[1] < $b[1]) ? -1 : 1;
}
$nations = array(
"Nation_Two" => array("2", "derp"),
"Nation_One" => array("1", "blah"),
"Nation_Four" => array("4", "deep"),
"Nation_Three" => array("3", "doop"),
"Nation_A" => array("9", "beep"),
"Nation_B" => array("0", "boop")
);
// Simple, built-in array sort
array_multisort($nations,SORT_ASC);
print_r($nations);
// Custom sort by first array index of each array element
usort($nations,"my_sort");
print_r($nations);
// Custom sort by second array index of each array element
usort($nations,"my_sort_string");
print_r($nations);
- The first sort (array_multisort) is ordering by the first index for each index of your nations array.
- The second sort (usort) is doing the same thing, except within the comparison function we are targeting a specific index.
- The third sort (also usort) is doing the same as the other previous usort, but on the second index of the nations array, thereby ordering alphabetically.
With these examples I just wanted to point out that you can get into some pretty creative sorting, there's really no one way to approach your problem.
Hope that helps,
Andy