user1950349
user1950349

Reputation: 5146

sort multidimensional array on the basis of a field in php

I have multidemensional arrary as shown below in which I want to do sorting on the basis of [name] field in php.

Array
(
    [chicago] => Array
        (
            [community_name] => Chicago, IL
            [areas] => Array
                (
                    [0] => Array
                        (
                            [name] => Array
                                (
                                    [0] => HELLO WORLD.
                                )
                        )

                    [1] => Array
                        (
                            [name] => Array
                                (
                                    [0] => Hello
                                )

                        )

                    [2] => Array
                        (
                            [name] => Array
                                (
                                    [0] => Administration.
                                )
                        )
                )

        )

    [chicago-and-surrounding-areas] => Array
        (
            [community_name] => Chicago (and surrounding areas), IL
            [areas] => Array
                (
                    [0] => Array
                        (
                            [name] => Array
                                (
                                    [0] => Carry.
                                )
                        )
                    [1] => Array
                        (
                            [name] => Array
                                (
                                    [0] => Bacteria.
                                )
                        )   
                )

        )

    [cambridge-chicago] => Array
        (
            [community_name] => Cambridge (Chicago), IL
            [areas] => Array
                (
                    [0] => Array
                        (
                            [name] => Array
                                (
                                    [0] => Responsibility.
                                )
                        )
                    [1] => Array
                        (
                            [name] => Array
                                (
                                    [0] => Bacteria.
                                )
                        )   
                )

        )

)

This is what I want to achieve:

Array
(
    [chicago] => Array
        (
            [community_name] => Chicago, IL
            [areas] => Array
                (
                    [0] => Array
                        (
                            [name] => Array
                                (
                                    [0] => Administration
                                )
                        )

                    [1] => Array
                        (
                            [name] => Array
                                (
                                    [0] => Hello
                                )

                        )

                    [2] => Array
                        (
                            [name] => Array
                                (
                                    [0] => HELLO WORLD.
                                )
                        )
                )

        )

    [chicago-and-surrounding-areas] => Array
        (
            [community_name] => Chicago (and surrounding areas), IL
            [areas] => Array
                (
                    [0] => Array
                        (
                            [name] => Array
                                (
                                    [0] => Bacteria.
                                )
                        )
                    [1] => Array
                        (
                            [name] => Array
                                (
                                    [0] => Carry.
                                )
                        )   
                )

        )

    [cambridge-chicago] => Array
        (
            [community_name] => Cambridge (Chicago), IL
            [areas] => Array
                (
                    [0] => Array
                        (
                            [name] => Array
                                (
                                    [0] => Bacteria.
                                )
                        )
                    [1] => Array
                        (
                            [name] => Array
                                (
                                    [0] => Responsibility.
                                )
                        )   
                )

        )

)

This is what I have tried but I think more need to be done.

function cmp($a, $b)
{
    if ($a["name"] == $b["name"]) {
        return 0;
    }
    return ($a["name"] < $b["name"]) ? -1 : 1;
}

usort($response,"cmp");

Problem Statement:

I am wondering what changes I need to make in the php code above so that it sorts an array on the basis of name field. With my php code above its not sorting anything. It is just printing the input as it is.

Upvotes: 1

Views: 59

Answers (1)

fubar
fubar

Reputation: 17388

Based on your example, you're actually trying to sort the areas sub-array, rather than the whole parent array. As such you'll need to loop through each sub-array in turn and sort them separately.

$array = [
    'chicago' => [
        'community_name' => 'Chicago, IL',
        'areas' => [
            [
                'name' => ['HELLO WORLD.']
            ],
            [
                'name' => ['Hello'],
            ],
            [
                'name' => ['Administration.'],
            ],
        ],
    ],
    'chicago-and-surrounding-areas' => [
        'community_name' => 'Chicago (and surrounding areas), IL',
        'areas' => [
            [
                'name' => ['Carry.']
            ],
            [
                'name' => ['Bacteria.'],
            ],
        ],
    ],
    'cambridge-chicago' => [
        'community_name' => 'Cambridge (Chicago), IL',
        'areas' => [
            [
                'name' => ['Responsibility.']
            ],
            [
                'name' => ['Bacteria'],
            ],
        ],
    ],
];

foreach ($array as &$locality) {
    usort($locality['areas'], function ($a, $b) {
        return $a['name'][0] <=> $b['name'][0];
    });
}

var_dump($array);

Working example

Upvotes: 1

Related Questions