Page 1 of 1

Sort multi-array

Posted: Tue Sep 18, 2012 12:11 pm
by mister_v
Hello,

I would like to sort a multidimensional-array on a column.
I tried sort(array), but it doesn't work on multi-arrays.

Re: Sort multi-array

Posted: Tue Sep 18, 2012 12:38 pm
by chris
I usually create my own function and use it with usort:

Code: Select all

function num_sort($a,$b)
{
    return strcmp($a[2],$b[2]);
}
And call it with

Code: Select all

usort($multi_array,"num_sort");
The array is in this case the second field named '2' (But you can give it any name you want).
$multi_array['key1'][1]="value 1";
$multi_array['key1'][2]="string to sort";
$multi_array['key2'][1]="other value";
$multi_array['key2'][2]="sort string";