using combination of array_map, array_column and array_reverse :
$myArrays = array(
array(1, 2, 3, 4),
array(5, 6, 7, 8),
array(9, 10, 11, 12),
array(13, 14, 15, 16)
);
$index = 0;
$myArrays = array_map(function ($v) use (&$index, $myArrays) {
if (($index % 2) != 0) {
echo implode("\n", array_reverse(array_column($myArrays, $index))) . "\n";
} else {
echo implode("\n", array_column($myArrays, $index)) . "\n";
}
$index++;
}, $myArrays);
live demo : https://3v4l.org/0m2jZ
using foreach instead of array_map
foreach ($myArrays as $index => $myArray) {
if (($index % 2) != 0) {
echo implode("\n", array_reverse(array_column($myArrays, $index))) . "\n";
} else {
echo implode("\n", array_column($myArrays, $index)) . "\n";
}
}
live demo : https://3v4l.org/RNC9R
both will output the following :
1
5
9
13
14
10
6
2
3
7
11
15
16
12
8
4
solved Multiple Array puzzle