PHP scheduling problem, look at the picture
A company had no idea after thinking about the interview questions for half an hour.
$arr = [
[4, 5, 9, 4, 1],
[11, 14, 9, 6, 20],
[21, 44, 90, 16, 21],
[16, 34, 99, 600, 230],
[121, 18, 89, 60, 33]
]
the array $arr [0] [0]
is the maximum, $arr [4] [4]
is the second largest, $arr [0] [1]
is the third largest, and so on, $arr [2] [2]
is the minimum
.
4,33,5,60,90
$arr = [
[4, 5, 9, 4, 1],
[11, 14, 9, 6, 20],
[21, 44, 90, 16, 21],
[16, 34, 99, 600, 230],
[121, 18, 89, 60, 33]
]
$arr1 = [];
foreach ($arr as $v) {
$arr1 = array_merge($arr1, $v);
}
rsort($arr1);
$arrA = [];
$arrB = [];
foreach ($arr1 as $key => $value) {
if ($key % 2 == 0) {
$arrA[] = $value;
} else {
array_unshift($arrB, $value);
}
}
$arrC = array_merge($arrA, $arrB);
return array_chunk($arrC, 5);
$arr = [
[4, 5, 9, 4, 1],
[11, 14, 9, 6, 20],
[21, 44, 90, 16, 21],
[16, 34, 99, 600, 230],
[121, 18, 89, 60, 33]
];
foreach ($arr as &$v) {
rsort($v);
$l = $r = [];
foreach ($v as $key => $value) {
if ($key & 1) {
array_unshift($r, $value);
} else {
$l[] = $value;
}
}
$v = array_merge($l, $r);
}
var_dump($arr);
made some changes based on @ Masterton
I don't know if this is the case
$arr = [
[4, 5, 9, 4, 1],
[11, 14, 9, 6, 20],
[21, 44, 90, 16, 21],
[16, 34, 99, 600, 230],
[121, 18, 89, 60, 33]
];
$result = array_reduce($arr, 'array_merge', array());
$count=count($result);
for($i=0;$i<$count;$iPP){
if($i%2==0){
$arrs[]+=$result[$i/2];
}else{
$arrs[]+=$result[$count-1-floor($i/2)];
}
}
print_r($arrs);