phpsortingmultidimensional-array

Sort multi dimensional array in php


I have the following array which I want to sort based on the "lastfirma" value. So the following array:

Array
(
    [000002] => Array
        (
            [clients] => Array
                (
                    [556046-2222] => Array
                        (
                            [lastfirma] => Name 1
                            [periods] => Array
                                (
                                    [1] => Array
                                        (
                                            [userId] => 000015
                                            [start] => 2020-05-01
                                            [end] => 2021-04-30
                                        )
                                    [2] => Array
                                        (
                                            [userId] => 105779
                                            [start] => 2021-05-01
                                            [end] => 2022-04-30
                                        )
                                )
                        )
                    [556892-1111] => Array
                        (
                            [lastfirma] => Anomaly
                             [periods] => Array
                                (
                                    [1] => Array
                                        (
                                            [userId] => 000015
                                            [start] => 2020-05-01
                                            [end] => 2021-04-30
                                        )
                                    [2] => Array
                                        (
                                            [userId] => 105779
                                            [start] => 2021-05-01
                                            [end] => 2022-04-30
                                        )
                                )
                        )
                )
        )
)

would become

Array
(
    [000002] => Array
        (
            [clients] => Array
                (
                    [556892-1111] => Array
                        (
                            [lastfirma] => Anomaly
                             [periods] => Array
                                (
                                    [1] => Array
                                        (
                                            [userId] => 000015
                                            [start] => 2020-05-01
                                            [end] => 2021-04-30
                                        )
                                    [2] => Array
                                        (
                                            [userId] => 105779
                                            [start] => 2021-05-01
                                            [end] => 2022-04-30
                                        )
                                )
                        )
                    [556046-2222] => Array
                        (
                            [lastfirma] => Name 1
                            [periods] => Array
                                (
                                    [1] => Array
                                        (
                                            [userId] => 000015
                                            [start] => 2020-05-01
                                            [end] => 2021-04-30
                                        )
                                    [2] => Array
                                        (
                                            [userId] => 105779
                                            [start] => 2021-05-01
                                            [end] => 2022-04-30
                                        )
                                )
                        )
                )
        )
)

Solution

  • uasort(
      $input['000002']['clients'],
      static fn($item1, $item2) => $item1['lastfirma'] <=> $item2['lastfirma']
    );
    

    If there is more than one key on the first level ('000002', '000003', ...), then wrap it in a foreach loop:

    foreach ($input as &$item) {
      uasort(
        $item['clients'],
        static fn($item1, $item2) => $item1['lastfirma'] <=> $item2['lastfirma']
      );
    }
    unset($item);
    

    Note the reference variable &$item, and the unsetting of the last reference after the for loop.