1

I have a multi dimensional array

Array
 (
[M] => Array
    (
        [0] => Array
            (
                [first] => Paul
                [last] => Martha
                [progress] => 100
                [id] => 85162
                [category] => M
            )

        [1] => Array
            (
                [first] => Bob
                [last] => Marley
                [progress] => 47
                [id] => 846523
                [category] => M
            )

    )

[M~F1] => Array
    (
        [0] => Array
            (
                [first] => Kenneth
                [last] => Rodriguez
                [progress] => 42
                [id] => 110273
                [category] => M~F1
            )

        [1] => Array
            (
                [first] => Jenifer
                [last] => Lopez
                [progress] => 0
                [id] => 485623
                [category] => M~F1
            )

    )

)

However, I want each of the smaller arrays to sort themselves alphabetically based on keys from he array inside of them. Take array M for example. I want PHP to organize its subcompenets by alphabetizing the Last name componet. So, array M would organize its subarrays by alphabetizing the subarray of the subbaray. Once complete, it should look like this:

Array
(
[M] => Array
    (
    [0] => Array
            (
                [first] => Bob
                [last] => Marley
                [progress] => 47
                [id] => 846523
                [category] => M
            )

        [1] => Array
            (
                [first] => Paul
                [last] => Martha
                [progress] => 100
                [id] => 85162
                [category] => M
            )

    )

[M~F1] => Array
    (
        [0] => Array
            (
        [first] => Jenifer
                [last] => Lopez
                [progress] => 0
                [id] => 485623
                [category] => M~F1
            )

        [1] => Array
            (
                [first] => Keneth
                [last] => Rodriguez
                [progress] => 42
                [id] => 110273
                [category] => M~F1
            )

    )

)

So, to review, I ned to organize the subsub array by looking at the subsubsub array, gathering its [last] key and sorting the subsub array from the alphabetization of the[last] key of the subsubsub array.

  • Possible duplicate of [How can I sort arrays and data in PHP?](http://stackoverflow.com/questions/17364127/how-can-i-sort-arrays-and-data-in-php) – miken32 May 29 '16 at 02:23

1 Answers1

0

Let the array you've defined be $x.

// first, define our comparison function
$cmp = function ($a, $b) {
  // sort by last name, first name
  $a = trim($a['last'] . ' ' . $a['first']);
  $b = trim($b['last'] . ' ' . $b['first']);
  return strcmp($a, $b);
};
foreach ($x as &$subx) {
  // initialize sub array with reference
  usort($subx, $cmp);
}
rposky
  • 2,206
  • 2
  • 17
  • 22