0
Array
(
    [12] => Array
        (
            [id] => 12
            [name] => Car
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 14
                            [name] => Volvo
                        )

                    [1] => Array
                        (
                            [id] => 15
                            [name] => Mercedes-Benz
                        )

                )

        )

    [13] => Array
        (
            [id] => 13
            [name] => Manga
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 16
                            [name] => Naruto
                        )

                    [1] => Array
                        (
                            [id] => 17
                            [name] => Hunter X Hunter
                        )

                )

        )

    [18] => Array
        (
            [id] => 18
            [name] => aa
            [children] => Array
                (
                )

        )



)

Guys I want to sort the values of this array, i want to sort it by key and the key is 'name'. This should sort the first level and the second level thru key 'name'.

This array i put in print_r() so it looks like this. The array is not fix so i can add in the future.

So after sorting the final value of the array is...

Array
(

    [18] => Array
        (
            [id] => 18
            [name] => aa
            [children] => Array
                (
                )

        )

    [12] => Array
        (
            [id] => 12
            [name] => Car
            [children] => Array
                (
            [0] => Array
                        (
                            [id] => 15
                            [name] => Mercedes-Benz
                        )

                    [1] => Array
                        (
                            [id] => 14
                            [name] => Volvo
                        )

            )

        )

    [13] => Array
        (
            [id] => 13
            [name] => Manga
            [children] => Array
                (
            [0] => Array
                        (
                            [id] => 17
                            [name] => Hunter X Hunter
                        )

                    [1] => Array
                        (
                            [id] => 16
                            [name] => Naruto
                        )

                )

        )
)



And this is the array in code.

$categories = array(
                12 => 
                    array('id' =>12, 
                              'name' => 'Car',
                                       'children' => 
                                        array(
                                            array('id' => 14,
                                                      'name' => 'volvo'
                                            )
                                        ),
                                        array(
                                            array('id' => 15,
                                                      'name' => 'Mercedez-Benz'
                                            )
                                        )
                            ),

                13 => 
                    array('id' =>13, 
                              'name' => 'Manga',
                                       'children' => 
                                        array(
                                            array('id' => 16,
                                                      'name' => 'Naruto'
                                            )
                                        ),
                                        array(
                                            array('id' => 17,
                                                      'name' => 'Hunter X Hunter'
                                            )
                                        )
                            ),

                18=>
                    array('id' => 18, 
                              'name'=> 'aa', 
                                      'children' => array())


                );

echo "<pre>";

print_r($categories);
Jon B
  • 51,025
  • 31
  • 133
  • 161
  • 3
    You can use http://php.net/manual/en/function.array-walk-recursive.php and save the the data in a new vector. – Alexandru Chelariu Nov 27 '12 at 13:23
  • possible duplicate of [Sort arrays into multidimensional array by key](http://stackoverflow.com/questions/904718/sort-arrays-into-multidimensional-array-by-key) – JB King Nov 27 '12 at 18:00

3 Answers3

0

the 'name' is not your real 'Key', just saying, because 'key' in Php normally means the name of your array, like '18' and '12' in your array and so on, like this:

<?php
    $fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
    ksort($fruits);
    foreach ($fruits as $key => $val) {
        echo "$key = $val\n";
    }
?>

in this example, the result will be:

a = orange
b = banana
c = apple
d = lemon

anyway to answer your question on how to sort it on 'name', use 'usort':

function cmp($a, $b) {
   return $a['name'] - $b['name'];
}

usort($arr,"cmp");

check out the following link for further information: How do I sort a PHP array by an element nested inside?

Community
  • 1
  • 1
Bananam00n
  • 814
  • 9
  • 27
0

if using php 5.3+ you can use closures in your code and sort like this

usort($categories,function($a,$b){
    return $a['name'] - $b['name']
}
NappingRabbit
  • 1,888
  • 1
  • 13
  • 18
0

So,

I'm going to try and explain it better to you

your array is big, yes, but you want to sort only on the key value '[name]'. In my test I used the array you provided:

$categories = array(
            12 => 
                array('id' =>12, 
                          'name' => 'Car',
                                   'children' => 
                                    array(
                                        array('id' => 14,
                                                  'name' => 'volvo'
                                        )
                                    ),
                                    array(
                                        array('id' => 15,
                                                  'name' => 'Mercedez-Benz'
                                        )
                                    )
                        ),

            13 => 
                array('id' =>13, 
                          'name' => 'Manga',
                                   'children' => 
                                    array(
                                        array('id' => 16,
                                                  'name' => 'Naruto'
                                        )
                                    ),
                                    array(
                                        array('id' => 17,
                                                  'name' => 'Hunter X Hunter'
                                        )
                                    )
                        ),

            18=>
                array('id' => 18, 
                          'name'=> 'aa', 
                                  'children' => array())


            );

if the array is declared, you can add the usort like this:

usort($categories,"sortByName");

the first element in the above ($categories), is your array, the second element will provide the name of a function, in this case 'sortByName'.

after that, you just add a function into your code (it doesn't really matter where you put it, even if it's at the bottom of the page):

function sortByName($categories, $b) {
    return strcasecmp($categories["name"], $b["name"]);
}

Basically what this function does, is compairing your array with ...your array :) so it can sort it alfabetically by name- like you want it. sthe 'strcasecmp' is to compare your strings against eachother to sort it. not that this is case non sensitive (strcmp will be case sensitive too). I assume you don't want it to check on uppercase and so on too, otherwise the result won't be what you wanted.

when you added that code, you can just print your array again:

echo "<pre>";
print_r($categories);

your result will be:

Array
(
[0] => Array
    (
        [id] => 18
        [name] => aa
        [children] => Array
            (
            )

    )

[1] => Array
    (
        [id] => 12
        [name] => Car
        [children] => Array
            (
                [0] => Array
                    (
                        [id] => 14
                        [name] => volvo
                    )

            )

        [0] => Array
            (
                [0] => Array
                    (
                        [id] => 15
                        [name] => Mercedez-Benz
                    )

            )

    )

[2] => Array
    (
        [id] => 13
        [name] => Manga
        [children] => Array
            (
                [0] => Array
                    (
                        [id] => 16
                        [name] => Naruto
                    )

            )

        [0] => Array
            (
                [0] => Array
                    (
                        [id] => 17
                        [name] => Hunter X Hunter
                    )

            )

    )

I hope this is what you wanted. :-)

Bananam00n
  • 814
  • 9
  • 27