-1

I have an array which looks like

Array
(
    [0] => Array
        (
            [id] => 39662
            [points] => 24
            [subject] => 112
        )

    [1] => Array
        (
            [id] => 39609
            [points] => 24
            [subject] => 87
        )

    [2] => Array
        (
            [id] => 39610
            [points] => 23
            [subject] => 77
        )

    [3] => Array
        (
            [id] => 39608
            [points] => 23
            [subject] => 87
        )

    [4] => Array
        (
            [id] => 39606
            [points] => 22
            [subject] => 60
        )

    [5] => Array
        (
            [id] => 39604
            [points] => 19
            [subject] => 75
        )

    [6] => Array
        (
            [id] => 39595
            [points] => 18
            [subject] => 60
        )

    [7] => Array
        (
            [id] => 39605
            [points] => 18
            [subject] => 47
        )

    [8] => Array
        (
            [id] => 39650
            [points] => 17
            [subject] => 87
        )

    [9] => Array
        (
            [id] => 39660
            [points] => 17
            [subject] => 55
        )

)

Now I want to sort then based on count of key subject. You can see that subjuet = 87 have 3 records and subject = 60 has two records, so all three records of 87 should display first , after this records of 60 , then others.

I tried array_multisort but its not giving expected result.

Thanks

Rich5757
  • 155
  • 2
  • 12

3 Answers3

1

As per your desired output, you just need to array_map() with array_multisort(),

Example:

<?php
// Test Array
$array = array(
    array('id'=>39662,'points'=>'24','subject'=>112),
    array('id'=>39609,'points'=>'24','subject'=>87),
    array('id'=>39610,'points'=>'23','subject'=>77),
    array('id'=>39608,'points'=>'23','subject'=>87),
    array('id'=>39606,'points'=>'22','subject'=>60),
    array('id'=>39604,'points'=>'19','subject'=>75),
    array('id'=>39595,'points'=>'18','subject'=>60),
    array('id'=>39605,'points'=>'18','subject'=>47),
    array('id'=>39650,'points'=>'17','subject'=>87),
    array('id'=>39660,'points'=>'17','subject'=>55),
  );

$newArr = array(); // initialize the new Array
foreach ($array as $key => $value)
{
    $newArr[$value['subject']][] = $value;
}

array_multisort(array_map('count', $newArr), SORT_DESC, $newArr); // using array_multisort() and Sort as DESC order by using array_map()

echo "<pre>";
print_r($newArr);
?>

Result:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [id] => 39609
                    [points] => 24
                    [subject] => 87
                )

            [1] => Array
                (
                    [id] => 39608
                    [points] => 23
                    [subject] => 87
                )

            [2] => Array
                (
                    [id] => 39650
                    [points] => 17
                    [subject] => 87
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [id] => 39606
                    [points] => 22
                    [subject] => 60
                )

            [1] => Array
                (
                    [id] => 39595
                    [points] => 18
                    [subject] => 60
                )

        )

    [2] => Array
        (
            [0] => Array
                (
                    [id] => 39604
                    [points] => 19
                    [subject] => 75
                )

        )

    [3] => Array
        (
            [0] => Array
                (
                    [id] => 39605
                    [points] => 18
                    [subject] => 47
                )

        )

    [4] => Array
        (
            [0] => Array
                (
                    [id] => 39610
                    [points] => 23
                    [subject] => 77
                )

        )

    [5] => Array
        (
            [0] => Array
                (
                    [id] => 39660
                    [points] => 17
                    [subject] => 55
                )

        )

    [6] => Array
        (
            [0] => Array
                (
                    [id] => 39662
                    [points] => 24
                    [subject] => 112
                )

        )

)
devpro
  • 16,184
  • 3
  • 27
  • 38
0

Try the following method for your case

$data[] = array('points' => 67, 'subject' => 2);
$data[] = array('points' => 86, 'subject' => 1);
$data[] = array('points' => 85, 'subject' => 6);
$data[] = array('points' => 98, 'subject' => 2);
$data[] = array('points' => 86, 'subject' => 6);
$data[] = array('points' => 67, 'subject' => 7);


// Obtain a list of columns
foreach ($data as $key => $row) {
    $subject[$key] = $row['subject'];
}

// Sort the data with volume descending, edition ascending
// Add $data as the last parameter, to sort by the common key
array_multisort($subject, SORT_DESC, $data);

Example usage with my array from the php manual.

Output of the above was

Array
(
    [0] => Array
        (
            [points] => 67
            [subject] => 7
        )

    [1] => Array
        (
            [points] => 85
            [subject] => 6
        )

    [2] => Array
        (
            [points] => 86
            [subject] => 6
        )

    [3] => Array
        (
            [points] => 67
            [subject] => 2
        )

    [4] => Array
        (
            [points] => 98
            [subject] => 2
        )

    [5] => Array
        (
            [points] => 86
            [subject] => 1
        )

)
Sasikumar
  • 863
  • 5
  • 9
0
function array_sort($array, $key){

    $new_array = array();
    $sortable_array = array();

    if (count($array) > 0) {
        foreach ($array as $k => $v) {
            if (is_array($v)) {
                foreach ($v as $k2 => $v2) {

                    if ($k2 == $key) {
                        $sortable_array[$k] = $v2;
                    }
                }
            } else {
                $sortable_array[$k] = $v;
            }
        }


       asort($sortable_array);


        }

        foreach ($sortable_array as $k => $v) {
            $new_array[$k] = $array[$k];
        }
    }

    return $new_array;
}
 array_sort($a, 'subjects');

//$a is array which want to sort

Chandra Sekar
  • 302
  • 1
  • 13