1

I should explain why my question is unique: My question is unique, because the proposed solutions are working for first instance of the array, but I want to know how I work with it for the next arrays. Thx

My PHP array $kategorien:

Array
(
    [0] => Array
        (
            [order] => 0
            [id] => 1
            [name] => Other
        )

    [1] => Array
        (
            [order] => 1
            [id] => 5652372018
            [name] => Templates & Addons
            [sub] => Array
                (
                    [0] => Array
                        (
                            [order] => 1
                            [id] => 13989101018
                            [name] => deutsch
                            [sub] => Array
                                (
                                    [0] => Array
                                        (
                                            [order] => 0
                                            [id] => 16694354018
                                            [name] => hübsch
                                        )

                                )

                        )

                    [1] => Array
                        (
                            [order] => 0
                            [id] => 13989102018
                            [name] => english
                        )

                )

        )

    [2] => Array
        (
            [order] => 0
            [id] => 13989097018
            [name] => Domains
        )

    [3] => Array
        (
            [order] => 2
            [id] => 15403616018
            [name] => Service
        )

)

I can sort this array by "order" with

function sortiere($a, $b)
          {
            return strcmp($a['order'], $b['order']);
          }
        usort($kategorien, 'sortiere')

or

foreach ($kategorien as $key => $row) {
            $order[$key]    = $row['order'];                
        }
        array_multisort($order, SORT_ASC, $kategorien);

Both works and I get

    order - 0
    id - 1
    name - Other

    order - 0
    id - 13989097018
    name - Domains

    order - 1
    id - 5652372018
    name - Templates & Addons

            order - 1
            id - 13989101018
            name - deutsch

                    order - 0
                    id - 16694354018
                    name - hübsch

            order - 0
            id - 13989102018
            name - english

    order - 2
    id - 15403616018
    name - Service

But I can not make to sort the next arrays. I need the same sorting by "order":

    order - 0
    id - 1
    name - Other

    order - 0
    id - 13989097018
    name - Domains

    order - 1
    id - 5652372018
    name - Templates & Addons

            order - 0          <------------------
            id - 13989102018
            name - english

            order - 1          <-------------------
            id - 13989101018
            name - deutsch

                    order - 0
                    id - 16694354018
                    name - hübsch

    order - 2
    id - 15403616018
    name - Service

How can I use the working functions for the next arrays? Thank You!

Narendrasingh Sisodia
  • 21,247
  • 6
  • 47
  • 54
  • 1
    possible duplicate of [How to sort an array of associative arrays by value of a given key in PHP?](http://stackoverflow.com/questions/1597736/how-to-sort-an-array-of-associative-arrays-by-value-of-a-given-key-in-php) – Sulthan Allaudeen May 22 '15 at 10:20
  • I dont looking for an answer to "how can I sort an multidimensional array". I have to sort the multidimensional array in a multidimensional array, but dont know how. :-( – Matthias from Berlin May 22 '15 at 10:40

1 Answers1

3

I would create a recursive function that will sort one level and re-called on the next level if [sub] exists and so on.

Here is my solution that will sort all the array in all levels by order value:

Code:

function sortiere($a, $b) { return strcmp($a['order'], $b['order']); }
function sort_by_order(&$arr) {
    usort($arr, 'sortiere');
    foreach ($arr as $k => $c) {
       if (isset($c['sub'])) {
           sort_by_order($arr[$k]['sub']);
       }
    }
    return;
}

Usage:

$arr = Array(
    /* the array with the specific structure as you showed */ 
);
sort_by_order($arr);
print_r($arr);

Demo:

online demo

Shlomi Hassid
  • 6,500
  • 3
  • 27
  • 48