-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

6
  • 1
    Give the code that you have tried to help you Commented Sep 21, 2016 at 12:58
  • 1
    I tried code on stackoverflow.com/questions/7433569/… . Both array_multisort and usort. Commented Sep 21, 2016 at 13:00
  • en.wikipedia.org/wiki/Insertion_sort Commented Sep 21, 2016 at 13:10
  • Possible duplicate of Sort Multi-dimensional Array by Value Commented Sep 21, 2016 at 13:15
  • @RaviHirani , I think the link you given is not giving right answer. We have to calculate count of subject run time and based on these count we will sort array. Commented Sep 21, 2016 at 13:18

3 Answers 3

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
                )

        )

)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @devpro , In above I just not need nested array . Elements should rearrange according to count.
@Rich5757: its rearranged
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
        )

)

1 Comment

In your example because subject = 6 has two records so these records should display on top.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.