嗨我有以下数组我将下面的列表转换为基于索引的新列表集...如水平到垂直方式
mainArray['a'] = [1,2,3,4];
mainArray['b'] = [4,5,6,7];
mainArray['c'] = [7,8,9,10];
mainArray['d'] = [10,11,12,13];
我需要简短的操作来使它像
[1,4,7,10][2,5,8,11][3,6,9,12][4,7,10,13]
答案 0 :(得分:0)
这可能就是你要找的东西:
<?php
$input = [
'a' => [1,2,3,4],
'b' => [4,5,6,7],
'c' => [7,8,9,10],
'd' => [10,11,12,13]
];
$output = [];
array_walk($input, function($entry) use (&$output) {
foreach ($entry as $key=>$val) {
$output[$key][] = $val;
}
});
print_r($output);
另一种选择是保守迭代(我认为由于各种原因它很难看):
<?php
$input = [
'a' => [1,2,3,4],
'b' => [4,5,6,7],
'c' => [7,8,9,10],
'd' => [10,11,12,13]
];
$output = [];
for ($i=0; $i<count($input['a']); $i++) {
$output[] = array_column($input, $i);
}
print_r($output);
上述代码的输出为:
Array
(
[0] => Array
(
[0] => 1
[1] => 4
[2] => 7
[3] => 10
)
[1] => Array
(
[0] => 2
[1] => 5
[2] => 8
[3] => 11
)
[2] => Array
(
[0] => 3
[1] => 6
[2] => 9
[3] => 12
)
[3] => Array
(
[0] => 4
[1] => 7
[2] => 10
[3] => 13
)
)