3

I have this multidimensional array:

Array
(
    [0] => Array
        (
        [0] => 2012-02-26 07:15:00
        )
    [1] => Array
        (
            [0] => 2012-02-26 17:45:00
            [1] => 2012-02-26 18:55:00
        )
    [2] => Array
        (
            [0] => 2012-02-26 18:55:00
            [1] => 2012-02-26 17:45:00
        )
    [3] => Array
        (
            [0] => 2012-02-26 18:57:00
            [1] => 2012-02-26 17:45:00
            [2] => 2012-02-26 18:55:00
        )

When I count subarrays I get this 1,2,2,3. How could I receive it in 3,2,2,1? I need to get for example last 3 subarrays with the highest subarray count (DESC, it means 3,2,2). How can I achieve this?

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
peter
  • 4,289
  • 12
  • 44
  • 67

4 Answers4

9

You can achieve it by utilizing usort function.

function cmp($a, $b){
    return (count($b) - count($a));
}
usort($array, 'cmp');
$highest_3_sub_arrays = array_slice($array, 0, 3);
Shiplu Mokaddim
  • 56,364
  • 17
  • 141
  • 187
3

This might be what you seek:

natsort($sub_count);
$rev = array_reverse($sub_count);
$result = array_pad($rev, 3);

You might want to omit the actual sorting if the values you have are already in order.

Mikulas Dite
  • 7,790
  • 9
  • 59
  • 99
1
$sizes=array();
foreach ($myarray as $k=>$v) 
  if (!is_array($v)) $sizes["$k"]=0;
  else $sizes["$k"]=sizeof($v);

sort($sizes);


echo array_pop($sizes); //outputs 3
echo array_pop($sizes); //outputs 2
echo array_pop($sizes); //outputs 2
Eugen Rieck
  • 64,175
  • 10
  • 70
  • 92
  • thanks, it works too. just rename echo to: print_r(array_pop($sizes)); //outputs 3 print_r(array_pop($sizes)); //outputs 2 print_r(array_pop($sizes)); //outputs 2 – peter Feb 26 '12 at 18:50
0

It seems to me that all of the other answers are working too hard. usort(), count(), and foreach() aren't necessary and when I tried natsort() it gave me: <b>Notice</b>: Array to string conversion in <b>[...][...]</b>.

rsort() will put the longest subarrays first.

Code:

$array=array(
    ["2012-02-26 18:55:00","2012-02-26 17:45:00"],
    ["2012-02-26 07:15:00"],
    ["2012-02-26 18:57:00","2012-02-26 17:45:00","2012-02-26 18:55:00"],
    ["2012-02-26 17:45:00","2012-02-26 18:55:00"]
);

$size=3; // modify this line to declare how many subarrays to capture
rsort($array); // sort the subarrays in DESC order
var_export(array_slice($array,0,$size));  // print the first n subarrays

Output:

array (
  0 => 
  array (
    0 => '2012-02-26 18:57:00',
    1 => '2012-02-26 17:45:00',
    2 => '2012-02-26 18:55:00',
  ),
  1 => 
  array (
    0 => '2012-02-26 18:55:00',
    1 => '2012-02-26 17:45:00',
  ),
  2 => 
  array (
    0 => '2012-02-26 17:45:00',
    1 => '2012-02-26 18:55:00',
  ),
)

If you want to implement some additional sorting to break the length-ties (like between your two 2-element subarrays), then you will need to specify that in your question.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • @peter I am revisiting my old answers to see if I can improve them. As far as I know, there isn't a better way than what I have posted. Did you consider my answer as a solution? – mickmackusa Dec 20 '19 at 22:21