我有一个数组:
GET
我需要打印输出:
Foo
答案 0 :(得分:0)
这里你的代码编写得非常糟糕,但适用于你的问题。数组维度必须为4 * 4.
<?php
$myArrays = array(
array(1, 2, 3, 4),
array(5, 6, 7, 8),
array(9, 10, 11, 12),
array(13, 14, 15, 16)
);
$counter1 = 0;
while($counter1 < 4)
{
echo $myArrays[$counter1][0];
echo "\n";
$counter1++;
}
$counter1 = 3;
while($counter1 >= 0)
{
echo $myArrays[$counter1][1];
echo "\n";
$counter1--;
}
$counter1 = 0;
while($counter1 < 4)
{
echo $myArrays[$counter1][2];
echo "\n";
$counter1++;
}
$counter1 = 3;
while($counter1 >= 0)
{
echo $myArrays[$counter1][3];
echo "\n";
$counter1--;
}
?>
答案 1 :(得分:0)
你可以尝试这是
$myArrays = array(
array(1, 2, 3, 4),
array(5, 6, 7, 8),
array(9, 10, 11, 12),
array(13, 14, 15, 16)
);
$i = 0;
foreach($myArrays as $key => $value){
$i = $key;
foreach($value as $keys =>$values){
$addArray[$i] = $values;
$i = $i+4;
}
}
ksort($addArray);
$arrays = array_chunk($addArray,4);
krsort($arrays[1]);
krsort($arrays[3]);
$final = array_merge($arrays[0],$arrays[1],$arrays[2],$arrays[3]);
foreach ( $final as $key => $value){
echo $value ."<br>";
}
答案 2 :(得分:0)
使用array_map,array_column和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);
使用foreach而不是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";
}
}
两者都将输出以下内容:
1
5
9
13
14
10
6
2
3
7
11
15
16
12
8
4