-5
Array ( [0] => Array ( [1] => Array ( [EmpId] => STU100 [EmpName] => XXXXXX [percentage] => [max_point] => [opt_point] => ) 
                            [2] => Array ( [EmpId] => STU100 [EmpName] => XXXXXX [percentage] => [max_point] => [opt_point] => ) 
                            [3] => Array ( [EmpId] => STU100 [EmpName] => XXXXXX [percentage] => [max_point] => [opt_point] => ) 
                            [4] => Array ( [EmpId] => STU100 [EmpName] => XXXXXX [percentage] => [max_point] => [opt_point] => ) 
                            [5] => Array ( [EmpId] => STU100 [EmpName] => XXXXXX [percentage] => [max_point] => [opt_point] => ) 
                            [6] => Array ( [EmpId] => STU100 [EmpName] => XXXXXX [percentage] => [max_point] => [opt_point] => ) 
                            [7] => Array ( [EmpId] => STU100 [EmpName] => XXXXXX [percentage] => [max_point] => [opt_point] => ) 
                            [8] => Array ( [EmpId] => STU100 [EmpName] => XXXXXX [percentage] => [max_point] => [opt_point] => ) 
                            [9] => Array ( [EmpId] => STU100 [EmpName] => XXXXXX [percentage] => [max_point] => [opt_point] => ) 
                            [10] => Array ( [EmpId] => STU100 [EmpName] => XXXXXX [percentage] => [max_point] => [opt_point] => ) 
                            [11] => Array ( [EmpId] => STU100 [EmpName] => XXXXXX [percentage] => [max_point] => [opt_point] => ) 
                            [12] => Array ( [EmpId] => STU100 [EmpName] => XXXXXX [percentage] => [max_point] => [opt_point] => ) )
            [1] => Array ( [1] => Array ( [EmpId] => STU101 [EmpName] => YYYYY [percentage] => [max_point] => [opt_point] => ) 
                            [2] => Array ( [EmpId] => STU101 [EmpName] => YYYYY [percentage] => [max_point] => [opt_point] => ) 
                            [3] => Array ( [EmpId] => STU101 [EmpName] => YYYYY [percentage] => [max_point] => [opt_point] => ) 
                            [4] => Array ( [EmpId] => STU101 [EmpName] => YYYYY [percentage] => [max_point] => [opt_point] => ) 
                            [5] => Array ( [EmpId] => STU101 [EmpName] => YYYYY [percentage] => [max_point] => [opt_point] => ) 
                            [6] => Array ( [EmpId] => STU101 [EmpName] => YYYYY [percentage] => [max_point] => [opt_point] => ) 
                            [7] => Array ( [EmpId] => STU101 [EmpName] => YYYYY [percentage] =>90 [max_point] => [opt_point] => ) 
                            [8] => Array ( [EmpId] => STU101 [EmpName] => YYYYY [percentage] => [max_point] => [opt_point] => ) 
                            [9] => Array ( [EmpId] => STU101 [EmpName] => YYYYY [percentage] => [max_point] => [opt_point] => ) 
                            [10] => Array ( [EmpId] => STU101 [EmpName] => YYYYY [percentage] => [max_point] => [opt_point] => ) 
                            [11] => Array ( [EmpId] => STU101 [EmpName] => YYYYY [percentage] => [max_point] => [opt_point] => ) 
                            [12] => Array ( [EmpId] => STU101 [EmpName] => YYYYY [percentage] => [max_point] => [opt_point] => )
             [2] => Array ( [1] => Array ( [EmpId] => STU102 [EmpName] => YYYYY [percentage] => [max_point] => [opt_point] => ) 
                            [2] => Array ( [EmpId] => STU102 [EmpName] => YYYYY [percentage] => [max_point] => [opt_point] => ) 
                            [3] => Array ( [EmpId] => STU102 [EmpName] => YYYYY [percentage] => [max_point] => [opt_point] => ) 
                            [4] => Array ( [EmpId] => STU102 [EmpName] => YYYYY [percentage] => [max_point] => [opt_point] => ) 
                            [5] => Array ( [EmpId] => STU102 [EmpName] => YYYYY [percentage] => [max_point] => [opt_point] => ) 
                            [6] => Array ( [EmpId] => STU102 [EmpName] => YYYYY [percentage] => [max_point] => [opt_point] => ) 
                            [7] => Array ( [EmpId] => STU102 [EmpName] => YYYYY [percentage] =>100 [max_point] => [opt_point] => ) 
                            [8] => Array ( [EmpId] => STU102 [EmpName] => YYYYY [percentage] => [max_point] => [opt_point] => ) 
                            [9] => Array ( [EmpId] => STU102 [EmpName] => YYYYY [percentage] => [max_point] => [opt_point] => ) 
                            [10] => Array ( [EmpId] => STU102 [EmpName] => YYYYY [percentage] => [max_point] => [opt_point] => ) 
                            [11] => Array ( [EmpId] => STU102 [EmpName] => YYYYY [percentage] => [max_point] => [opt_point] => ) 
                            [12] => Array ( [EmpId] => STU102 [EmpName] => YYYYY [percentage] => [max_point] => [opt_point] => ) ).....

I need the array sorted based on [percentage], ex: STU102 was 1st,STU101 was 2nd and STU100 was 3rd based on these manner i need to get the output, Can any one help me. I tried, When i use this means,

function cmp($a, $b) {
  return $b[7]["percentage"] - $a[7]["percentage"];
}
usort($per_details1, "cmp"); 

it was sorted. But i give $b[7]["percentage"] - $a[7]["percentage"] 7 th index directly.

Ivan
  • 34,531
  • 8
  • 55
  • 100
Babu
  • 1
  • 2

4 Answers4

1

Use usort in php along with arraywalk

function sort($a, $b) {
    if ($a['percentage'] == $b['percentage']) return 0;
    return ($a['percentage'] > $b['percentage']) ? 1 : -1;
}

function sort(&$value) {
    usort($value, 'sort');
}

array_walk($array, 'sort');
Satyapal Sharma
  • 303
  • 3
  • 11
  • You may want to change the name of your function `sort` to something else like `mySort` because `sort` a built-in function and this code would raise an error..... ;-) – Poiz Jul 19 '16 at 08:14
  • How does this sort by the `percentage` field in the array? – Barmar Jul 19 '16 at 08:20
  • I use this code my dear friend, but not getting an exact result. – Babu Jul 19 '16 at 08:24
  • downvoted because it doesn't attempt to sort by `percentage` as the OP requested. – BeetleJuice Jul 19 '16 at 11:52
  • @BeetleJuice changed the code to sort by percentage, missed that part before – Satyapal Sharma Jul 19 '16 at 12:05
  • removed the downvote – BeetleJuice Jul 19 '16 at 12:16
  • @Poiz Valid concern, But I was just trying to point in the right direction not providing the exact code :P – Satyapal Sharma Jul 20 '16 at 06:59
  • @SatyapalSharma Perfect!!! It was also imagined that, "that was your intention"... however the concern was only because the OP might not see it that way since he's probably a beginner judging from his question & SO Reputation and anyways +10 for you ;-) – Poiz Jul 20 '16 at 07:02
1

Loop over your array and use usort() to sort them by the percentage field in the sub-arrays. Use a reference variable in the foreach loop so it operates on the arrays in place.

foreach ($array as &$subarray) {
    usort ($subarray, function ($a, $b) {
        return $a['percentage'] - $b['percentage'];
    }
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • would you have any kind of relationship with oBArMAr?.....Seriously, it's an honest Curiosity..... It's just that you look like an Authority Figure ;-) – Poiz Jul 19 '16 at 12:27
1

With a blend of array_walk and usort; you can get the job done. The Code below illustrates how.

In Brief: to get the job done

    <?php

        // FIRST; SORT THE MAIN ARRAY IN REVERSE ORDER...
        rsort($mdArray);

        // IF YOU WANT TO SORT BY ANY OTHER FIELD, SAY "EmpId"
        // YOU CAN SIMPLY CHANGE THE DEFAULT VALUE OF $field to "EmpId"
        function sortByField($prev, $next, $field="percentage") {
            return $next[$field] - $prev[$field];
        }

        array_walk($mdArray, function(&$data){
            usort($data, "sortByField");
        });

In details: with a sample Array:

    <?php
        $mdArray    = array(
            array (
                array( "EmpId" => "STU100", "EmpName" => "XXXXXX", "percentage" => 15, "max_point" => null, "opt_point" => null, ),
                array( "EmpId" => "STU100", "EmpName" => "XXXXXX", "percentage" => 78, "max_point" => null, "opt_point" => null, ),
                array( "EmpId" => "STU100", "EmpName" => "XXXXXX", "percentage" => 20 ,"max_point" => null, "opt_point" => null, ),
                array( "EmpId" => "STU100", "EmpName" => "XXXXXX", "percentage" => 10, "max_point" => null, "opt_point" => null, ),
                array( "EmpId" => "STU100", "EmpName" => "XXXXXX", "percentage" => 77, "max_point" => null, "opt_point" => null, ),
                array( "EmpId" => "STU100", "EmpName" => "XXXXXX", "percentage" => 34, "max_point" => null, "opt_point" => null, ),
                array( "EmpId" => "STU100", "EmpName" => "XXXXXX", "percentage" => 22, "max_point" => null, "opt_point" => null, ),
                array( "EmpId" => "STU100", "EmpName" => "XXXXXX", "percentage" => 78, "max_point" => null, "opt_point" => null, ),
                array( "EmpId" => "STU100", "EmpName" => "XXXXXX", "percentage" => 49, "max_point" => null, "opt_point" => null, ),
                array( "EmpId" => "STU100", "EmpName" => "XXXXXX", "percentage" => 55, "max_point" => null, "opt_point" => null, ),
                array( "EmpId" => "STU100", "EmpName" => "XXXXXX", "percentage" => 36, "max_point" => null, "opt_point" => null, ),
                array( "EmpId" => "STU100", "EmpName" => "XXXXXX", "percentage" => 18, "max_point" => null, "opt_point" =>  null,)
            ),
            array (
                array( "EmpId" => "STU101", "EmpName" => "YYYYY", "percentage" => 70, "max_point" => null, "opt_point" => null, ),
                array( "EmpId" => "STU101", "EmpName" => "YYYYY", "percentage" => 54, "max_point" => null, "opt_point" => null, ),
                array( "EmpId" => "STU101", "EmpName" => "YYYYY", "percentage" => 23, "max_point" => null, "opt_point" => null, ),
                array( "EmpId" => "STU101", "EmpName" => "YYYYY", "percentage" => 46, "max_point" => null, "opt_point" => null, ),
                array( "EmpId" => "STU101", "EmpName" => "YYYYY", "percentage" => 10, "max_point" => null, "opt_point" => null, ),
                array( "EmpId" => "STU101", "EmpName" => "YYYYY", "percentage" => 21, "max_point" => null, "opt_point" => null, ),
                array( "EmpId" => "STU101", "EmpName" => "YYYYY", "percentage" => 80, "max_point" => null, "opt_point" => null, ),
                array( "EmpId" => "STU101", "EmpName" => "YYYYY", "percentage" => 34, "max_point" => null, "opt_point" => null, ),
                array( "EmpId" => "STU101", "EmpName" => "YYYYY", "percentage" => 50, "max_point" => null, "opt_point" => null, ),
                array( "EmpId" => "STU101", "EmpName" => "YYYYY", "percentage" => 27, "max_point" => null, "opt_point" => null, ),
                array( "EmpId" => "STU101", "EmpName" => "YYYYY", "percentage" => 39, "max_point" => null, "opt_point" => null, ),
                array( "EmpId" => "STU101", "EmpName" => "YYYYY", "percentage" => 12, "max_point" => null, "opt_point" => null, ),
            ),
        );


        // FIRST; SORT THE MAIN ARRAY IN REVERSE ORDER...
        rsort($mdArray);

        // IF YOU WANT TO SORT BY ANY OTHER FIELD, SAY "EmpId"
        // YOU CAN SIMPLY CHANGE THE DEFAULT VALUE OF $field to "EmpId"
        function sortByField($prev, $next, $field="percentage") {
            return $next[$field] - $prev[$field];
        }

        array_walk($mdArray, function(&$data){
            usort($data, "sortByField");
        });

        var_dump($mdArray);

The var_dump above produces something like the result below. Notice, now, that the array is sorted by percentages in Descending Order. If you want to reverse the process (ie. sort in Ascending Order), simply swap $next and $prev within (inside) the function: sortByField.

        array (size=2)
          0 => 
            array (size=12)
              0 => 
                array (size=5)
                  'EmpId' => string 'STU100' (length=6)
                  'EmpName' => string 'XXXXXX' (length=6)
                  'percentage' => int 78
                  'max_point' => null
                  'opt_point' => null
              1 => 
                array (size=5)
                  'EmpId' => string 'STU100' (length=6)
                  'EmpName' => string 'XXXXXX' (length=6)
                  'percentage' => int 78
                  'max_point' => null
                  'opt_point' => null
              2 => 
                array (size=5)
                  'EmpId' => string 'STU100' (length=6)
                  'EmpName' => string 'XXXXXX' (length=6)
                  'percentage' => int 77
                  'max_point' => null
                  'opt_point' => null
              3 => 
                array (size=5)
                  'EmpId' => string 'STU100' (length=6)
                  'EmpName' => string 'XXXXXX' (length=6)
                  'percentage' => int 55
                  'max_point' => null
                  'opt_point' => null
              4 => 
                array (size=5)
                  'EmpId' => string 'STU100' (length=6)
                  'EmpName' => string 'XXXXXX' (length=6)
                  'percentage' => int 49
                  'max_point' => null
                  'opt_point' => null
              5 => 
                array (size=5)
                  'EmpId' => string 'STU100' (length=6)
                  'EmpName' => string 'XXXXXX' (length=6)
                  'percentage' => int 36
                  'max_point' => null
                  'opt_point' => null
              6 => 
                array (size=5)
                  'EmpId' => string 'STU100' (length=6)
                  'EmpName' => string 'XXXXXX' (length=6)
                  'percentage' => int 34
                  'max_point' => null
                  'opt_point' => null
              7 => 
                array (size=5)
                  'EmpId' => string 'STU100' (length=6)
                  'EmpName' => string 'XXXXXX' (length=6)
                  'percentage' => int 22
                  'max_point' => null
                  'opt_point' => null
              8 => 
                array (size=5)
                  'EmpId' => string 'STU100' (length=6)
                  'EmpName' => string 'XXXXXX' (length=6)
                  'percentage' => int 20
                  'max_point' => null
                  'opt_point' => null
              9 => 
                array (size=5)
                  'EmpId' => string 'STU100' (length=6)
                  'EmpName' => string 'XXXXXX' (length=6)
                  'percentage' => int 18
                  'max_point' => null
                  'opt_point' => null
              10 => 
                array (size=5)
                  'EmpId' => string 'STU100' (length=6)
                  'EmpName' => string 'XXXXXX' (length=6)
                  'percentage' => int 15
                  'max_point' => null
                  'opt_point' => null
              11 => 
                array (size=5)
                  'EmpId' => string 'STU100' (length=6)
                  'EmpName' => string 'XXXXXX' (length=6)
                  'percentage' => int 10
                  'max_point' => null
                  'opt_point' => null
          1 => 
            array (size=12)
              0 => 
                array (size=5)
                  'EmpId' => string 'STU101' (length=6)
                  'EmpName' => string 'YYYYY' (length=5)
                  'percentage' => int 80
                  'max_point' => null
                  'opt_point' => null
              1 => 
                array (size=5)
                  'EmpId' => string 'STU101' (length=6)
                  'EmpName' => string 'YYYYY' (length=5)
                  'percentage' => int 70
                  'max_point' => null
                  'opt_point' => null
              2 => 
                array (size=5)
                  'EmpId' => string 'STU101' (length=6)
                  'EmpName' => string 'YYYYY' (length=5)
                  'percentage' => int 54
                  'max_point' => null
                  'opt_point' => null
              3 => 
                array (size=5)
                  'EmpId' => string 'STU101' (length=6)
                  'EmpName' => string 'YYYYY' (length=5)
                  'percentage' => int 50
                  'max_point' => null
                  'opt_point' => null
              4 => 
                array (size=5)
                  'EmpId' => string 'STU101' (length=6)
                  'EmpName' => string 'YYYYY' (length=5)
                  'percentage' => int 46
                  'max_point' => null
                  'opt_point' => null
              5 => 
                array (size=5)
                  'EmpId' => string 'STU101' (length=6)
                  'EmpName' => string 'YYYYY' (length=5)
                  'percentage' => int 39
                  'max_point' => null
                  'opt_point' => null
              6 => 
                array (size=5)
                  'EmpId' => string 'STU101' (length=6)
                  'EmpName' => string 'YYYYY' (length=5)
                  'percentage' => int 34
                  'max_point' => null
                  'opt_point' => null
              7 => 
                array (size=5)
                  'EmpId' => string 'STU101' (length=6)
                  'EmpName' => string 'YYYYY' (length=5)
                  'percentage' => int 27
                  'max_point' => null
                  'opt_point' => null
              8 => 
                array (size=5)
                  'EmpId' => string 'STU101' (length=6)
                  'EmpName' => string 'YYYYY' (length=5)
                  'percentage' => int 23
                  'max_point' => null
                  'opt_point' => null
              9 => 
                array (size=5)
                  'EmpId' => string 'STU101' (length=6)
                  'EmpName' => string 'YYYYY' (length=5)
                  'percentage' => int 21
                  'max_point' => null
                  'opt_point' => null
              10 => 
                array (size=5)
                  'EmpId' => string 'STU101' (length=6)
                  'EmpName' => string 'YYYYY' (length=5)
                  'percentage' => int 12
                  'max_point' => null
                  'opt_point' => null
              11 => 
                array (size=5)
                  'EmpId' => string 'STU101' (length=6)
                  'EmpName' => string 'YYYYY' (length=5)
                  'percentage' => int 10
                  'max_point' => null
                  'opt_point' => null

Test it out here.

Poiz
  • 7,611
  • 2
  • 15
  • 17
  • Thank You Friend, I need the 2 nd array to 1st first to second like that ex: STU101 should came first,STU100 came 2nd like that i need the array... Can you please help me... – Babu Jul 19 '16 at 09:14
  • @Babu You may try out the updated code again... (if you wish) – Poiz Jul 19 '16 at 09:31
  • @Babu That's why SO exists.... ;-) You may want to check/mark the answer as the right one , though (if you wish) – Poiz Jul 19 '16 at 10:22
  • Still i am not get the result... – Babu Jul 19 '16 at 10:33
0

You really should format your code when you post it.

You can use PHP's usort to accomplish what you're after. See the accepted answer here.

function cmp($a, $b) {
    return $a["mid"] - $b["mid"];
}
usort($arr, "cmp");

Also, while we are happy to help here on SO, you should do some of your own research into your problem as well.

Community
  • 1
  • 1
Zac Brown
  • 459
  • 4
  • 12
  • function cmp($a, $b) { return $a["mid"] - $b["mid"]; } usort($arr, "cmp"); I already used this but not get the result, my array i like array inside array... – Babu Jul 19 '16 at 08:02
  • Can you answer my Question. – Babu Jul 19 '16 at 10:30