6

I'm struggling on this one. I have an array that contains countries and regions. I want to sort both sets of information in ascending order on the key.

Here is the array I'm working with:

Array
(
    [Country] => Array
        (
            [United Kingdom] => Array
                (
                    [London] => Array
                        (
                            [0] => 1
                            [1] => 5
                            [2] => 23
                            [3] => 71
                        )

                    [Manchester] => Array
                        (
                            [0] => 800
                        )

                )

            [United States] => Array
                (
                    [New York] => Array
                        (
                            [0] => 147
                            [1] => 111
                        )

                    [Washington] => Array
                        (
                            [0] => 213
                        )

                    [Florida] => Array
                        (
                            [0] => 6
                        )

                    [Texas] => Array
                        (
                            [0] => 9
                        )

                )

            [Brazil] => Array
                (
                    [Brasília] => Array
                        (
                            [0] => 64
                        )

                )

        )

)

So the reordered array would be:

Brazil
- Brasília

United Kingdom
- London
- Manchester

United States
- Florida
- New York
- Texas
- Washington

The data structure should remain the same, but the order of the number (e.g. London: 1,5,23,71) can stay the same.

I've tried several of the sorting methods from: http://php.net/manual/en/array.sorting.php

But they dont appear to do anything. Maybe because its a multidimensional array or maybe its not structured 100% logically... but I'm stuck with the array as it is.

2
  • Just for clarification purposes: Do you want the UK cities ordered in reverse order (M before L)? Or is that just a typo? Commented Mar 27, 2013 at 20:47
  • Hi Steve. Apologies, that was indeed a typo. I've corrected it now :) Commented Mar 27, 2013 at 21:23

2 Answers 2

5

You can try:

ksort_recursive($data);
print_r($data);

Function Used

function ksort_recursive(&$array) {
    ksort($array);
    foreach ( $array as &$a ) {
        is_array($a) && ksort_recursive($a);
    }
}

See Testing on Multiple PHP Versions

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Baba. Your answer also works great. I accepted Steve's as it doesn't use a custom function and has fewer lines, but I really appreciate your answer :)
2

Step 1:
Sort the country by key.

ksort($arr['Country']);

Step 2: Loop through the countries and sort those keys.

foreach ($arr['Country'] as $country=>$data) {
    ksort($arr['Country'][$country]);
}

1 Comment

Thanks Steve. I've made a couple of slight changes, but that is working a treat for me. Thanks :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.