0

I have a multidimensional array in PHP that I am trying to alphabetize. I am trying to sort the students by alphabetical order by last name. Students are listed inside of $array['students'], but the ['submissions'] inside of each student needs to stay with that student after the sort is over.

I have tried using uasort($array['students'], 'cmp') but this does not return anything at all.

Basically, I need to alphabetize the students array. I'm not sure how to do this since I would be using a value to organize, and since this is a multidimensional array. Any help would be appreciated.

Array structure:

$array['assignments'][assignmentID][details]

$array['students'][studentID][details]

$array['students'][studentID]['submissions'][assignmentID][details]

Here is the output from print_r($array)

Array
(
    [assignments] => Array
        (
            [9] => Array
                (
                    [assEmoji] => ✏️
                    [title] => Roanoke Story Map
                    [points] => 10
                    [assigned] => 2021-08-16
                    [due] => 2021-08-20 15:00:00
                )

        )

    [students] => Array
        (
            [11] => Array
                (
                    [firstName] => John
                    [lastName] => Smith
                    [fullName] => John Smith
                    [earned] => 103
                    [maxpts] => 120
                    [submissions] => Array
                        (
                            [9] => Array
                                (
                                    [workID] => 539
                                    [status] => graded
                                    [submitted] => 2021-08-17 08:15:48
                                    [method] => wall
                                    [score] => 9
                                    [points] => 10
                                    [graded] => 2021-09-22 10:26:54
                                )

                        )

                )

                [12] => Array
                    (
                        [firstName] => Amy
                        [lastName] => Appleton
                        [fullName] => Amy Appleton
                        [earned] => 91
                        [maxpts] => 110
                        [submissions] => Array
                            (
                                [9] => Array
                                    (
                                        [workID] => 540
                                        [status] => graded
                                        [submitted] => 2021-08-17 08:45:48
                                        [method] => wall
                                        [score] => 7
                                        [points] => 10
                                        [graded] => 2021-09-22 10:28:54
                                    )

                            )

                    )

                    [13] => Array
                        (
                            [firstName] => Zeke
                            [lastName] => Lee
                            [fullName] => Zeke Lee
                            [earned] => 91
                            [maxpts] => 110
                            [submissions] => Array
                                (
                                    [9] => Array
                                        (
                                            [workID] => 540
                                            [status] => graded
                                            [submitted] => 2021-08-17 08:45:48
                                            [method] => wall
                                            [score] => 7
                                            [points] => 10
                                            [graded] => 2021-09-22 10:28:54
                                        )

                                )

                        )

        )

)
Mathew
  • 113
  • 2
  • 8
  • What have you tried so far? You should be able to use [`uasort()`](https://www.php.net/manual/en/function.uasort.php) to do this. – rickdenhaan Sep 29 '21 at 21:05
  • @rickdenhaan no I've tried that. I edited the question. I have tried using ```uasort($array['students'], 'cmp')``` but this does not return anything at all. I'm not sure where to move from here.. – Mathew Sep 29 '21 at 22:02
  • The second parameter of `uasort` should not be a string (unless the string is the name of a previously defined function). It needs to be a comparison function. Try `function($a, $b) { return strcasecmp($a['lastName'], $b['lastName']); }`. – Don't Panic Sep 29 '21 at 22:04
  • `uasort` takes the array it's sorting by reference, and this will apply to arrays within arrays as well, so you are correct in using `$array['students']`. – Don't Panic Sep 29 '21 at 22:06
  • 1
    If you got `uasort($array['students'], 'cmp')` from the example in the PHP manual page that rickdenhaan linked, you can see in the first part of that example where they defined the cmp function before they used it. If you make a named function like that, you can use a string to refer to it in uasort. The example I gave in my earlier comment is an anonymous function that you can use directly in uasort instead. – Don't Panic Sep 29 '21 at 22:13

0 Answers0