Matthias from Berlin
Matthias from Berlin

Reputation: 985

Sorting arrays inside an array by individual keys

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!

Upvotes: 1

Views: 95

Answers (1)

Shlomi Hassid
Shlomi Hassid

Reputation: 6606

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

Upvotes: 3

Related Questions