Sorting multidimensional array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Asmodeux
Forum Newbie
Posts: 9
Joined: Fri Apr 25, 2008 10:21 am

Sorting multidimensional array

Post 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?>);
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Sorting multidimensional array

Post 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 ...
There are 10 types of people in this world, those who understand binary and those who don't
Asmodeux
Forum Newbie
Posts: 9
Joined: Fri Apr 25, 2008 10:21 am

Re: Sorting multidimensional array

Post 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.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Sorting multidimensional array

Post 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.
There are 10 types of people in this world, those who understand binary and those who don't
Asmodeux
Forum Newbie
Posts: 9
Joined: Fri Apr 25, 2008 10:21 am

Re: Sorting multidimensional array

Post 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;
Asmodeux
Forum Newbie
Posts: 9
Joined: Fri Apr 25, 2008 10:21 am

Re: Sorting multidimensional array

Post 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);
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Sorting multidimensional array

Post 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 ...
There are 10 types of people in this world, those who understand binary and those who don't
Asmodeux
Forum Newbie
Posts: 9
Joined: Fri Apr 25, 2008 10:21 am

Re: Sorting multidimensional array

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Sorting multidimensional array

Post by John Cartwright »

I prefer to use usort() to define my own sorting rules.
Asmodeux
Forum Newbie
Posts: 9
Joined: Fri Apr 25, 2008 10:21 am

Re: Sorting multidimensional array

Post by Asmodeux »

I'm pretty n00b in the php world. I'll have to check that one too.

Thanks to all :drunk:
Post Reply