Page 1 of 1

Sorting multidimensional array

Posted: Fri Apr 25, 2008 10:38 am
by Asmodeux
I know this has been asked before, but reading the doc of the function and reading the posts here I still don't understand how to use the array_multisort or usort functions. :cry:

I have this array which is pretty simple

$array[0][0] = 'Jack';
$array[0][1] = 'Thomas';
$array[0][2] = '1981-04-11';
$array[1][0] = 'Gandalf';
$array[1][1] = 'Smith';
$array[1][2] = '1122-04-11';
$array[2][0] = 'Georges';
$array[2][1] = 'G.';
$array[2][2] = '1943-04-11';

I want to sort this DESCENDING by the date so it ends like that

$array[0][0] = 'Jack';
$array[0][1] = 'Thomas';
$array[0][2] = '1981-04-11';
$array[1][0] = 'Georges';
$array[1][1] = 'G.';
$array[1][2] = '1943-04-11';
$array[2][0] = 'Gandalf';
$array[2][1] = 'Smith';
$array[2][2] = '1122-04-11';

I guess a line like one of the above should do but I don't know what to put in place of <what goes here?>)

$rssSortedArray = array_multisort($array, <what goes here?>);
OR
$rssSortedArray = usort($array, <what goes here?>);

Re: Sorting multidimensional array

Posted: Fri Apr 25, 2008 11:10 am
by VladSun

Code: Select all

 
$array[0][0] = 'Jack';
$array[0][1] = 'Thomas';
$array[0][2] = '1981-04-11';
$array[1][0] = 'Gandalf';
$array[1][1] = 'Smith';
$array[1][2] = '1122-04-11';
$array[2][0] = 'Georges';
$array[2][1] = 'G.';
$array[2][2] = '1943-04-11';
 
$rssSortedArray = array_multisort($array[0], $array[2], $array[1]);
 
That means: order $array sub arrays by [0] values, then if there are equal [0] values order them by [2] values, if there are equal [0] values order them by [1] values...

PS: I think
Example #1 Sorting multiple arrays in the array_multisort manual on php.net is pretty clear ...

Re: Sorting multidimensional array

Posted: Fri Apr 25, 2008 12:06 pm
by Asmodeux
That doesn't work. I receive the following error.

Argument #1 is expected to be an array or a sort flag

I also tried

$rssSortedArray = array_multisort($array, $array[2]);

Doesn't work either.

Re: Sorting multidimensional array

Posted: Fri Apr 25, 2008 12:25 pm
by VladSun
I don't believe you ;)

Code: Select all

 
$array[0][0] = 'Jack';
$array[0][1] = 'Thomas';
$array[0][2] = '1981-04-11';
$array[1][0] = 'Gandalf';
$array[1][1] = 'Smith';
$array[1][2] = '1122-04-11';
$array[2][0] = 'Georges';
$array[2][1] = 'G.';
$array[2][2] = '1943-04-11';
 
array_multisort($array[0], $array[1], $array[2]);
 
print_r($array);
 
output:

Code: Select all

 
Array
(
    [0] => Array
        (
            [0] => 1981-04-11
            [1] => Jack
            [2] => Thomas
        )
 
    [1] => Array
        (
            [0] => 1122-04-11
            [1] => Gandalf
            [2] => Smith
        )
 
    [2] => Array
        (
            [0] => 1943-04-11
            [1] => Georges
            [2] => G.
        )
 
)
 
My only mistake was that array_multisort() doesn't retyrn the sorted array, but rather a boolean result indicating success or not. But it wouldn't raise the error you said you had.

Re: Sorting multidimensional array

Posted: Fri Apr 25, 2008 12:40 pm
by Asmodeux
I'm sure it's supposed to work but...

I'm still receiving that error

Warning: array_multisort() [function.array-multisort]: Argument #1 is expected to be an array or a sort flag

Here is my call.

array_multisort($array["birthDate"]);

My array is not only [x][y] it's build like this and so on...the second index being a string.

$($array[$itemCount]["birthDate"] = $birthDate;

Re: Sorting multidimensional array

Posted: Fri Apr 25, 2008 1:02 pm
by Asmodeux
Seems all (almost) the examples I stumbled upon were missing something and they were all (7 different sites) exact copy/paste from the php.net site... What the...? People are so lazy those days.

Thanks VladSun but thanks to THIS post : http://burden.ca/blog/2008/03/27/array_ ... our-friend

Needed to create the array of indexes before launching the sorting. In the following case, $birthDate is not the array we sort. The result array is the last parameter. In this case I overwrite the same array.

$array[0]['firstName'] = 'Jack';
$array[0]['Name'] = 'Thomas';
$array[0]['birthDate'] = '1981-04-11';
array[1]['firstName'] = 'Gandalf';
$array[1]['Name'] = 'Smith';
$array[1]['birthDate'] = '1122-04-11';
$array[2]['firstName'] = 'Georges';
$array[2]['Name'] = 'G.';
$array[2]['birthDate'] = '1943-04-11';

foreach ($arrayas $key => $row)
{
$birthDate[$key] = $row['birthDate'];
}

array_multisort($birthDate, SORT_DESC, $array);

Re: Sorting multidimensional array

Posted: Fri Apr 25, 2008 1:03 pm
by VladSun
So... you need
Example #3 Sorting database results
...

And what you've written wont work ... you are sorting 1D array of birthdates only... You need to "rotate" the 2D array you have fetched from the DB ...

Re: Sorting multidimensional array

Posted: Fri Apr 25, 2008 1:11 pm
by Asmodeux
You still helped me here. No offense.

Sorry. But numbers instead of Strings would've been the same. If I take back my first example, it would've been the same.

$array[0][0] = 'Jack';
$array[0][1] = 'Thomas';
$array[0][2] = '1981-04-11';
$array[1][0] = 'Gandalf';
$array[1][1] = 'Smith';
$array[1][2] = '1122-04-11';
$array[2][0] = 'Georges';
$array[2][1] = 'G.';
$array[2][2] = '1943-04-11';

foreach ($array as $key => $row)
{
$birthDate[$key] = $row[2];
}

array_multisort($birthDate, SORT_DESC, $array);

I was just trying to be as generic as possible to help others.

Re: Sorting multidimensional array

Posted: Fri Apr 25, 2008 1:13 pm
by John Cartwright
I prefer to use usort() to define my own sorting rules.

Re: Sorting multidimensional array

Posted: Fri Apr 25, 2008 1:15 pm
by Asmodeux
I'm pretty n00b in the php world. I'll have to check that one too.

Thanks to all :drunk: