-1

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
                                        )
                                )
                        )
                )
        )
)
Kevin Lindmark
  • 1,155
  • 3
  • 13
  • 26
  • What's your question about this? What did you try to resolve your problem? Where are you stuck? – Nico Haase Aug 20 '23 at 11:08
  • Tried to apply the following after searching solutions but believe I want to sort on a key further down the dimension it doesn't work: $key_values = array_column($newarray, 'lastfirma'); array_multisort($key_values, SORT_ASC, $newarray); – Kevin Lindmark Aug 20 '23 at 11:13
  • Also tried the following suggestion: array_multisort(array_map(function($element) { return $element[4]['lastfirma']; }, $newarray), SORT_ASC, $newarray); – Kevin Lindmark Aug 20 '23 at 11:20
  • @KevinLindmark Always share `var_export` of your input data in your question. – nice_dev Aug 20 '23 at 12:11
  • `uasort($data['000002']['clients'], fn($a, $b) => strcmp($a['lastfirma'], $b['lastfirma']));` – Nick Aug 20 '23 at 12:13
  • https://3v4l.org/c5FIq – Nick Aug 20 '23 at 12:19
  • If you want to share new details about your problems, please add them to your question by editing it, not to the comment section – Nico Haase Aug 20 '23 at 12:28

2 Answers2

2
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.

lukas.j
  • 6,453
  • 2
  • 5
  • 24
2

An easy way to do this would be to use uasort():

uasort($array['000002']['clients'], fn ($a, $b) => strcmp($a['lastfirma'], $b['lastfirma']));

The above assumes that the lastfirma value is a string value and sorts the clients array by this value in ascending order and maintain index association.

Cyclonecode
  • 29,115
  • 11
  • 72
  • 93