Unseen
Unseen

Reputation: 27

PHP Sort Multidimensional Array by a sub sub-key

I have looked at numerous threads relating to this and none of them have been of any help to me. I have an array which follows the basic structure of $array[location][store][person] = funds. What is the most efficient way of sorting the array so that the [person] key is in ASC order?

This is what it looks like now:

Array
(
    [Florida] => Array
        (
            [AppleSauce] => Array
                (
                    [Rabbit, Hunting] => 5
                    [Brown, Bubba] => 20
                    [Chicken, Cantina] => 10
                    [Gum, Bubble] => 10
                    [Pool, Swimming] => 4
                    [Bath, Taka] => 2
                )

        )

    [Texas] => Array
        (
            [BeatleJuice] => Array
                (
                    [Chicken, Cantina] => 10
                    [Pool, Swimming] => 4
                    [House, Road] => 5
                )
            [CaramelApple] => Array
                (
                    [Chicken, Cantina] => 10
                    [Pool, Swimming] => 4
                    [House, Road] => 5
                )

        )

This is what I am looking for:

Array
    (
        [Florida] => Array
            (
                [AppleSauce] => Array
                    (
                        [Bath, Taka] => 2
                        [Brown, Bubba] => 20
                        [Chicken, Cantina] => 10
                        [Gum, Bubble] => 10
                        [Pool, Swimming] => 4
                        [Rabbit, Hunting] => 5
                    )

            )

        [Texas] => Array
            (
                [BeatleJuice] => Array
                    (
                        [Chicken, Cantina] => 10
                        [House, Road] => 5
                        [Pool, Swimming] => 4
                    )
                [CaramelApple] => Array
                    (
                        [Chicken, Cantina] => 10
                        [House, Road] => 5
                        [Pool, Swimming] => 4
                    )

            )

Upvotes: 1

Views: 221

Answers (2)

Sylvain Martin
Sylvain Martin

Reputation: 2375

The php function array_multisort can do this:

Sorting multi-dimensional array

<?php
$ar = array(
       array("10", 11, 100, 100, "a"),
       array(   1,  2, "2",   3,   1)
      );
array_multisort($ar[0], SORT_ASC, SORT_STRING,
                $ar[1], SORT_NUMERIC, SORT_DESC);
var_dump($ar);
?>

In this example, after sorting, the first array will transform to "10", 100, 100, 11, "a" (it was sorted as strings in ascending order). The second will contain 1, 3, "2", 2, 1 (sorted as numbers, in descending order).

array(2) {
  [0]=> array(5) {
    [0]=> string(2) "10"
    [1]=> int(100)
    [2]=> int(100)
    [3]=> int(11)
    [4]=> string(1) "a"
  }
  [1]=> array(5) {
    [0]=> int(1)
    [1]=> int(3)
    [2]=> string(1) "2"
    [3]=> int(2)
    [4]=> int(1)
  }
}

This is based from the official documentation:

http://php.net/manual/en/function.array-multisort.php

Upvotes: 0

FuzzyTree
FuzzyTree

Reputation: 32392

You can use ksort to sort the array keys of people in alphabetical order

foreach($array as $state => $locations) {
    foreach($locations as $location => $people) {
        ksort($array[$state][$location]);
    }
}

Upvotes: 1

Related Questions