Thursday, 30 August 2018

PHP Sort Array By SubArray Value by 3rd level but with clues


I'm totally desperate.


How can I sort an array in 3rd level that has an ID in the second level?
I want to sort this array only for "cars" and then ascending by "age"
array (
    ['cars'] => array (
        [23] => array(
            ['age'] => 10,
            ['color'] => 'yellow',
        ),
        [16] => array(
            ['age'] => 12,
            ['color'] => 'purple'
        ),
        [45] => array(
            ['age'] => 3,
            ['color'] => 'silver'
        )
    ),
    ['pages'] => array (
        [0] => array (
            ['number'] => 2
        )
    )
)


You can use uasort.
uasort($a['cars'], function($a, $b) {
   return $a['age'] > $b['age'] ? 1 : -1;
});


0 comments:

Post a Comment