1

I found this code for sorting:

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

It's good for one level. I have something like that:

array(
 array(
     'level'=>'aaa',
     'order'='1',
     'subs'=>array(
         array(
             'level'=>'bbb',
             'order'='1',
             'subs'=>array(
                 array(
                     'level'=>'ccc',
                     'order'='1',
                     'subs'=>array(
                         array(
                             'level'=>'ddd',
                             'order'='1',
                             'subs'=>array(
                                 ...
                             )
                         )
                     )
                 )
                 ,
                 array(
                     'level'=>'ccc',
                     'order'='2',
                     'subs'=>array(

                     )
                 )
             ),
         array(
             'level'=>'bbb',
             'order'='2'
         ),
         array(
             'level'=>'bbb',
             'order'='3'
         )
         )
     )
 ),
 array(
     'level'=>'aaa',
     'order'='2',
     'subs'=>array(

     )
 )

 )

Array may have any depth and any number of elements in each level. I need to sort each level of depth (aaa,bbb,ccc, etc) using the code above by key 'order'.

2
  • Why do you actually need it sorted? Commented Aug 7, 2014 at 21:25
  • I am writing menu generator. Menu generates from such array. And order in each level is important. Commented Aug 7, 2014 at 21:27

2 Answers 2

1

You will need to do this recursively.

recursive_sort($arr, $func) {
    foreach ($arr as $key => $val) {
        if (is_array($val)) {
            recursive_sort($val, $func);
        }
    }
    usort($arr, $func);
}

This code will iterate over the given array, and for each value that is an array, call itself with the value. The result is that usort will be called for every array within the structure.

You would call the function the same as you would usort:

recursive_sort($array, function($a, $b) {
    return $a['order_inside_level'] - $b['order_inside_level'];
});
Sign up to request clarification or add additional context in comments.

4 Comments

There are arrays of arrays [array(0=>array(),1=>array(),...)] in my example. They are arrays. It means they will be sorted by 'order', but they have no key 'order' (they are not associative arrays, they just have numeric indexes) Maby that is the reason of 'illegal string offset'?..
I cant make usort work inside of function: function fs($array){ return usort($array, 'sortByOrder');}; fs($myarray); - doesn't work. Without function it works: usort($myarray, 'sortByOrder'); How is that possible?..
sandbox.onlinephpfunctions.com/code/… - here is example. Usort doesn't work inside function. It works outside function (uncomment 95 string). So that your code doesn't work too...
I've decided the problem about work inside of function. Now sorting happens only in the first level of array - sandbox.onlinephpfunctions.com/code/…
1
function recursive_sort(&$arr) {
            fs($arr);
        foreach($arr as $k=> &$v){
            if (isset($v['subs'])) {
                 recursive_sort($v['subs']);
             }
        }
}


function sortByOrder($a, $b) {
    return $a['order_inside_level'] - $b['order_inside_level'];
}

function fs(&$array){

    usort($array, 'sortByOrder');

}

After multiple tries I have this. It works. Name of key (subs) is hardcoded what is not so good, but... I am glad it works.

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.