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