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
2

2 Answers 2

1

Use usort() to sort the array:

foreach($data as &$value) {
    usort($value,function($a,$b) {
        return $b['sort'] - $a['sort'];
    });
}
Sign up to request clarification or add additional context in comments.

2 Comments

@LouisW if the field you're sorting by isn't a string, you should use the updated code
^^ 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 */})
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'];                                         
    });                                                                                                                                       
}                                                                               

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.