0

I want to sort an array by number ( count() ) of inner-array items descending (i.e. most items first)

This is my array,

Array
(
    [Itm01] => Array
        (
            [0] => Array
                (
                    [id] => 238
                    [col1] => 7908
                    [col2] => 7181
                )

            [1] => Array
                (
                    [id] => 17024
                    [col1] => 1039
                    [col2] => 7181
                )

            [2] => Array
                (
                    [id] => 
                    [col1] => 1039
                    [col2] => 7181
                )

        )

    [Itm02] => Array
        (
            [0] => Array
                (
                    [id] => 260
                    [col1] => 1039
                    [col2] => 8964
                )

            [2] => Array
                (
                    [id] => 238
                    [col1] => 9149
                    [col2] => 8964
                )

            [3] => Array
                (
                    [id] => 238
                    [col1] => 0
                    [col2] => 8964
                )

            [4] => Array
                (
                    [id] => 238
                    [col1] => 7333
                    [col2] => 8964
                )

            [5] => Array
                (
                    [id] => 238
                    [col1] => 9049
                    [col2] => 8964
                )

            [6] => Array
                (
                    [id] => 238
                    [col1] => 7333
                    [col2] => 8964
                )

            [7] => Array
                (
                    [id] => 238
                    [col1] => 9049
                    [col2] => 8964
                )

            [8] => Array
                (
                    [id] => 238
                    [col1] => 8217
                    [col2] => 8964
                )

            [9] => Array
                (
                    [id] => 238
                    [col1] => 7516
                    [col2] => 8964
                )

            [10] => Array
                (
                    [id] => 82
                    [col1] => 1039
                    [col2] => 8964
                )

        )

    [Itm03] => Array
        (
            [0] => Array
                (
                    [id] => 238
                    [col1] => 7908
                    [col2] => 7276
                )

            [1] => Array
                (
                    [id] => 238
                    [col1] => 9049
                    [col2] => 7276
                )

        )

    [Itm04] => Array
        (
            [0] => Array
                (
                    [id] => 82
                    [col1] => 8217
                    [col2] => 8217
                )

        )

)

But I want item with index 'Itm02' as the first since it has more items. To be honest I want the same thing as this thread. But when I did this,

$warehouses = uksort($warehouses, function($a, $b) { return count($b) - count($a); });

But it returns an empty array. It would be great if someone can help.

vimuth
  • 5,064
  • 33
  • 79
  • 116
  • 1
    Uksort sorts array in place, does not return it. After `uksort($warehouses,..` the array is already sorted – splash58 Sep 18 '19 at 12:42

2 Answers2

2

uksort() returns a boolean.

Try the following:

uksort($warehouses, function($a, $b) { return count($b) - count($a); });

print_r($warehouses);
fonini
  • 3,243
  • 5
  • 30
  • 53
1

array_multisort() : Sort multiple or multi-dimensional arrays

array_multisort(array_map('count', $a), SORT_DESC, $a);

Working example : https://3v4l.org/EATkk

Rakesh Jakhar
  • 6,380
  • 2
  • 11
  • 20