Question

I have an array like this

Array
(
    [0] => Array
        (
            ['id'] => 130
            ['point'] => 45

        )

    [1] => Array
        (
             ['id'] => 190
            ['point'] => 30
        )

    [2] => Array
        (
            ['id'] => 145
            ['point'] => 79
        )
    [3] => Array
        (
            ['id'] => 178
            ['point'] => 40
        )
    [4] => Array
        (
            ['id'] => 189
            ['point'] => 79
        )
    [5] => Array
        (
            ['id'] => 132
            ['point'] => 9
        )

)

I want to sort the each three elements of this array separately on point in descending order.

Taking the above example

I want to sort [0],[1],[2] of the above array and [3],[4],[5] separately on the point so the final output will be

Array
    (
        [0] => Array
            (
                ['id'] => 145
                ['point'] => 79

            )

        [1] => Array
            (
                ['id'] => 130
                ['point'] => 45
            )

        [2] => Array
            (
                 ['id'] => 190
                 ['point'] => 30
            )
        [3] => Array
            (
                ['id'] => 189
                ['point'] => 79
            )
        [4] => Array
            (
                 ['id'] => 178
                 ['point'] => 40
            )
        [5] => Array
            (
                ['id'] => 132
                ['point'] => 9
            )

    )

I have tried some codes, but I couldn't get this to work, Please help. thanks in advance

Was it helpful?

Solution 2

Try usort:

 usort($arr, function($e1, $e2) {
      return $e1['point'] < $e2['point'];
 });

Complete solution:

 $splits = array_chunk($arr, 3);
 $result = array();
 foreach ($splits as $split) {
    usort($split, function($e1, $e2) {
            return $e1['point'] < $e2['point'];
    });
    $result = array_merge($result, $split);
 }

 print_r($result);

OTHER TIPS

Here's one way to do it:

  • Split the array into two chunks using array_chunk()
  • Store the arrays in two different variables
  • Define a custom sorting function to use as a callback for usort()
  • Sort the two arrays using the above-defined callback function
  • Join both the arrays using array_merge() to obtain the result array

Code:

// split the array into two chunks of 3
$chunks = array_chunk($data, 3);

// assign them into separate array variables
$firstArr = $chunks[0];
$secondArr = $chunks[1];

// define the sort function
function cmp($a, $b){
    return ($a['point'] > $b['point']) ? -1 : 1;
}

// sort the two arrays
usort($firstArr, 'cmp');
usort($secondArr, 'cmp');

// join them back to obtain the result
$result = array_merge($firstArr, $secondArr);

print_r($result);

A condensed version of the above code that will take care of multiple chunks:

$result = array();
foreach (array_chunk($data, 3) as $chunk) {
    usort($chunk, function($a, $b) {
        return $a['point'] < $b['point'];
    });
    $result = array_merge($result, $chunk);
}

Online demo

try this

$arr_temp1 = array();
$arr_temp2 = array();
foreach($your_array as $key=>$arr)
{
    if($key<3)
    {
        $arr_temp1[] = $arr;
    } 
    else 
    {
        $arr_temp2[] = $arr;
    }
}

usort($arr_temp1, function($a1, $a2) {
      return $a1['point'] < $a2['point'];
 });

usort($arr_temp2, function($a1, $a2) {
      return $a1['point'] > $a2['point'];
 });

$arr_new = array();
foreach($arr_temp1 as $arr)
{
   $arr_new[] = $arr;
}

foreach($arr_temp2 as $arr)
{
   $arr_new[] = $arr;
}

print_r($arr_new);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top