0

I have an associative array and inside that array each item has an array.

I want to sort the array by the number in the array for each item, how would I go about doing this?

Here is the array, and to clarify, it should be sorted so that it reads like this: C, A, D, B.

$array = array(
    "A" => array(
        "word" => "Apple",
        "number" => 945
    ),
    "B" => array(
        "word" => "Banana",
        "number" => 5698
    ),
    "C" => array(
        "word" => "Cherry",
        "number" => 12
    ),
    "D" => array(
        "word" => "Date",
        "number" => 1034
    )
);
Connor Wyatt
  • 166
  • 11

1 Answers1

2

There's a nice function, it's called uasort().

uasort($array, function($a, $b) { return $a["number"] - $b["number"]; });
bwoebi
  • 23,637
  • 5
  • 58
  • 79