0

I have an multidimensional array like this:

array ([0] => array ([id] => 1 [name] => john doe [title] => Mr [days] => 10) 
[1] => array ([id] => 2 [name] => Joe Smith [title] => Dr [days] => 22) 
[2] => array ([id] => 3 [name] => John Jones [title] => Mr [days] => 3))

I need to sort the inner arrays so that the data is returned in natural order by the days key.

I.E like this:

array ([2] => array ([id] => 3 [name] => John Jones [title] => Mr [days] => 3)
[0] => array ([id] => 1 [name] => john doe [title] => Mr [days] => 10) 
[1] => array ([id] => 2 [name] => Joe Smith [title] => Dr [days] => 22))

I think what I need is a function that applies natsort to a multidimensional array by $key, but so far I haven't been able to find any functions that do anything other than standard sorting.

Any help?

YsoL8
  • 2,186
  • 5
  • 29
  • 47
  • The usort solution works for me. I tried both methods and met no success. Then I examined the array's I was creating in detail and relised there were some unexpected features. Ironing those out usort worked immediately. Morale of the lesson is make sure you're working with what you think is there. – YsoL8 Sep 13 '10 at 11:56
  • "The obvious makes you overlook the evidence" --Sara Sidle – Dennis Haarbrink Sep 13 '10 at 12:15

3 Answers3

3

What you want is usort.

You can write a callback to do the comparison for you:

usort($data, function($a, $b) {
    return ($a['days'] > $b['days'])
           ? 1 
           : ($a['days'] < $b['days'])
             ? -1 
             : 0;
});

Disclaimer: You need PHP 5.3.x for this to work, else you have to resort to create_function or predefine the compare function.

Dennis Haarbrink
  • 3,738
  • 1
  • 27
  • 54
2

The following worked for me,

usort($array, function($a, $b) {
    return strnatcasecmp($a['days'], $b['days']);
});

Found from here :- https://learntech.imsu.ox.ac.uk/blog/php-naturally-sorting-multidimension-arrays-by-string-values/

Hope someone will find this useful.

Anjana Silva
  • 8,353
  • 4
  • 51
  • 54
0

A bit of a different approach:

$days = array();
foreach($array as $key => $val) {
    $days[$key] = $val['days'];
}
array_multisort($days, $array); //$array being your input array

Result:

Array
(
    [0] => Array
        (
            [id] => 3
            [name] => Jay Doe
            [title] => Mr
            [days] => 3
        )

    [1] => Array
        (
            [id] => 1
            [name] => John Doe
            [title] => Mr
            [days] => 10
        )

    [2] => Array
        (
            [id] => 2
            [name] => Joe Doe
            [title] => Mr
            [days] => 22
        )

)
Russell Dias
  • 70,980
  • 5
  • 54
  • 71
  • Can you clarify $array? Is that the array that needs sorting? (seen it) – YsoL8 Sep 13 '10 at 11:34
  • Yep. The one that you have in the question above. Simply assign that to $array, or any variable for that matter, just make sure you make the necessary changes. – Russell Dias Sep 13 '10 at 11:37