Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

What's the easiest way to sort a multi dimensional array. I need to sort it so the output is displayed with the highest count of values on the sub arrays then the sorted alphabetically by the key of the sub arrays:

So something like this:

MyArray(
  subarray4('val1')
  subarray2('val1')
  subarray3('val1', 'val2', 'val3')
  subarray1('val1', 'val2', 'val3')
)

Would look more like this:

MyArray(
  subarray1('val1', 'val2', 'val3')
  subarray3('val1', 'val2', 'val3')
  subarray2('val1')
  subarray4('val1')
)
share|improve this question
1  
Duplicate? "PHP Sort a multidimensional array by number of items" stackoverflow.com/questions/7433569/… and "Sorting array by count of subarray" stackoverflow.com/questions/14704634/… –  bloodyKnuckles Jul 3 at 11:11
    
neither of those help as they are sorting by subarrays key number, I want to sort by the number of values in each sub array. –  drewjuk Jul 3 at 14:21

2 Answers 2

up vote 0 down vote accepted

Two steps here, first sort array according to subarray count, then loop through array sorting each subarray "alphanumerically".

$myArray = array(
  array('val1', 'val2'),
  array('val3'),
  array('val7', 'val6', 'val5', 'val4'),
  array('val8', 'val9', 'val10'),
);

// sort primary array by subarray count
uasort($myArray, function ($a, $b) {
    $a = count($a);
    $b = count($b);
    return ($a == $b) ? 0 : (($a > $b) ? -1 : 1);
});

// now sort each subarray alphanumerically
foreach ( $myArray as $key => $arr ) {
    natsort($myArray[$key]); // letters - alpha, numbers - numeric
    // note: ignoring $arr because sorting it does not affect $myArray
}

print '<pre>';
print_r($myArray);
print '</pre>';

Output:

Array
(
    [2] => Array
        (
            [3] => val4
            [2] => val5
            [1] => val6
            [0] => val7
        )

    [3] => Array
        (
            [0] => val8
            [1] => val9
            [2] => val10
        )

    [0] => Array
        (
            [0] => val1
            [1] => val2
        )

    [1] => Array
        (
            [0] => val3
        )

)

The uasort sorting algorithm is cut and paste from here: Sorting array by count of subarray

share|improve this answer
1  
Thanks a bunch worked great, had to add is_array to second sort, as the subs with single items threw errors –  drewjuk Jul 3 at 20:53
$array = array(
    array('val1'),
    array('val1'),
    array('val1', 'val2', 'val3'),
    array('val1', 'val2', 'val3'),
);

function sortByCount($a, $b) {
    $a = count($a);
    $b = count($b);

    if($a == $b) {
        return 0;
    }

    return ($a > $b) ? -1 : 1;
}

uasort($array, 'sortByCount');
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.