Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP sort array alphabetically

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.

like image 517
iltdev Avatar asked Mar 27 '13 20:03

iltdev


People also ask

How do you sort an array in alphabetical order?

JavaScript Array sort() The sort() sorts the elements of an array. The sort() overwrites the original array. The sort() sorts the elements as strings in alphabetical and ascending order.

How can I sort an array in PHP without sort method?

php function sortArray() { $inputArray = array(8, 2, 7, 4, 5); $outArray = array(); for($x=1; $x<=100; $x++) { if (in_array($x, $inputArray)) { array_push($outArray, $x); } } return $outArray; } $sortArray = sortArray(); foreach ($sortArray as $value) { echo $value . "<br />"; } ?>

How do you sort an array of arrays in PHP?

To PHP sort array by key, you should use ksort() (for ascending order) or krsort() (for descending order). To PHP sort array by value, you will need functions asort() and arsort() (for ascending and descending orders).


2 Answers

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

like image 180
Baba Avatar answered Oct 05 '22 10:10

Baba


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]);
}
like image 24
Trenton Trama Avatar answered Oct 05 '22 10:10

Trenton Trama