2
$data = array(
    'apple' => array(
        0 => array('sort'=>4, 'name'=>'apple_4'),
        1 => array('sort'=>10, 'name'=>'apple_10'),
        2 => array('sort'=>5, 'name'=>'apple_5'),
        3 => array('sort'=>1, 'name'=>'apple_1')
        ),

    'orange' => array(
        0 => array('sort'=>4, 'name'=>'orange_4'),
        1 => array('sort'=>10, 'name'=>'orange_10')
        )
    );

Need assistance sorting multi-dimensional array. For the array above, I would like to sort the contents of each group in descending order by the 'sort' value. The group's keys should remain in tact (apple, orange) but content's keys are not important.

Data should be ordered:

  • apple
    • apple_10
    • apple_5
    • apple_4
    • apple_1
  • orange
    • orange_10
    • orange_4
Louis W
  • 2,950
  • 5
  • 39
  • 70
  • Have you tried something ? – Rizier123 Aug 01 '15 at 00:45
  • possible duplicate of [Reference: all basic ways to sort arrays and data in PHP](http://stackoverflow.com/questions/17364127/reference-all-basic-ways-to-sort-arrays-and-data-in-php) – baao Aug 01 '15 at 00:57

2 Answers2

1

Use usort() to sort the array:

foreach($data as &$value) {
    usort($value,function($a,$b) {
        return $b['sort'] - $a['sort'];
    });
}
Rizier123
  • 56,111
  • 16
  • 85
  • 130
FuzzyTree
  • 30,038
  • 3
  • 46
  • 73
  • @LouisW if the field you're sorting by isn't a string, you should use the updated code – FuzzyTree Aug 01 '15 at 00:54
  • 1
    ^^ Will remove the other comments, since everything is cleared. (An alternative to the foreach loop to add this here would be to use `array_walk()`, e.g. `array_walk($data, function(&$value){/* usort() here */}`) – Rizier123 Aug 01 '15 at 01:03
0
$data = array(                                                                  
    'apple' => array(                                                           
        0 => array('sort'=>4, 'name'=>'apple_4'),                               
        1 => array('sort'=>10, 'name'=>'apple_10'),                             
        2 => array('sort'=>5, 'name'=>'apple_5'),                               
        3 => array('sort'=>1, 'name'=>'apple_1')                                
    ),                                                                          

    'orange' => array(                                                          
        0 => array('sort'=>4, 'name'=>'orange_4'),                              
        1 => array('sort'=>10, 'name'=>'orange_10')                             
    )                                                                           
);                                                                              

foreach($data as &$value) {                                                        
    usort($value, function($a, $b) {                                            
        return $a['sort'] < $b['sort'];                                         
    });                                                                                                                                       
}                                                                               
Absalón Valdés
  • 884
  • 7
  • 16