-1

Possible Duplicate:
How to sort the following PHP multidimensional array?

Let's say I have following array:

Array
    (
        [0] => Array
            (
                [id] => 5
                [name] => Education
                [parent] => 1
                [sort_order] => 3
                [leaf] => 1
            )

        [1] => Array
            (
                [id] => 6
                [name] => Computers
                [parent] => 1
                [sort_order] => 1
                [leaf] => 1
            )

        [2] => Array
            (
                [id] => 7
                [name] => Science
                [parent] => 1
                [sort_order] => 2
                [children] => Array
                    (
                        [0] => Array
                            (
                                [id] => 8
                                [name] => Mathematics
                                [parent] => 7
                                [sort_order] => 2
                                [children] => Array
                                    (
                                        [0] => Array
                                            (
                                                [id] => 11
                                                [name] => Geometry
                                                [parent] => 8
                                                [sort_order] => 2
                                                [leaf] => 1
                                            )

                                        [1] => Array
                                            (
                                                [id] => 12
                                                [name] => Algebra
                                                [parent] => 8
                                                [sort_order] => 1
                                                [leaf] => 1
                                            )

                                    )

                            )

                        [1] => Array
                            (
                                [id] => 9
                                [name] => Physics
                                [parent] => 7
                                [sort_order] => 1
                                [leaf] => 1
                            )

                        [2] => Array
                            (
                                [id] => 10
                                [name] => Biology
                                [parent] => 7
                                [sort_order] => 4
                                [leaf] => 1
                            )

                        [3] => Array
                            (
                                [id] => 13
                                [name] => History
                                [parent] => 7
                                [sort_order] => 3
                                [leaf] => 1
                            )

                    )

            )

Is there any PHP built-in function which will sort this array separately on each level by 'sort_order' value? I have found several sollutions, but all of them sorted alphabetically or have used ksort().

There will be recursion needed probably, but I need some help how to start that. TIA.

Community
  • 1
  • 1
user1292810
  • 1,702
  • 7
  • 19
  • 38
  • What should happen for duplicate cases? Do you not like alphabetical because 10 will come before 2, or what? – Ally Dec 01 '12 at 15:25
  • @Ally I am preventing duplicate cases. `sort_order` on each level is unique. – user1292810 Dec 01 '12 at 15:32
  • 1
    possible duplicate of [How to sort the following PHP multidimensional array?](http://stackoverflow.com/questions/12900539/how-to-sort-the-following-php-multidimensional-array) or http://stackoverflow.com/questions/10998830/sort-php-multidimensional-array-by-value or http://stackoverflow.com/questions/13613286/sorting-a-big-multidimensional-array or http://stackoverflow.com/questions/13148243/sort-multi-dimensional-array-php or http://stackoverflow.com/questions/12006771/sort-multi-dimensional-array,http://stackoverflow.com/questions/12450304/sort-multidimensional-array-data-into-numerical-orde – PeeHaa Dec 01 '12 at 15:32

1 Answers1

0

there is one : usort it allows you to constomize the comparison function.

Sort 1 level

What is a comparison function ? a function taking two arguments and returning -1 when the first is lower than the second, +1 when the first is greater and 0 if both are equal.

How to define a function?

if there is a built-in function or a personnal function that bring the functionnality so the string that represents the function name is the point.

if it is an object's method, you have to use an array with a reference over the instance and the method name

and at least, you can use anonymous functions like this :

function sortALevel(&$array){

usort($array,function($a,$b){
     if($a['sort_order'] > $b['sort_order']){
       return 1;
     }
     if($a['sort_order'] < $b['sort_order']){
       return -1;
     }
     return 0;

  });
  }

sort the whole array

function sort_recursive($array){ 
sortALevel($array);
foreach($array as $value){
   if(is_array($value)){

     sort_recursive($value);
   }
}
}
artragis
  • 3,677
  • 1
  • 18
  • 30