1

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
                    )

            )
2
  • According to the PHP manual, it looks like asort() should work. php.net/manual/en/function.asort.php Commented Aug 6, 2015 at 20:25
  • @the_pete I've tried asort(), uksort(), ksort(), and array_multisort(), and I can't get any of them to work. According to the manual they should work, but how do you apply it to a multidimensional array like the one I have? Commented Aug 6, 2015 at 20:28

2 Answers 2

1

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]);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. That was exactly what I was looking for. I must have had something wrong when I tried it earlier.
0

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

1 Comment

Thanks, but that's not quite as efficient as what FuzzyTree showed.

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.