我有以下数组:
Array
(
[xx] => Array
(
[0] => 3
[1] => 10
[2] => 1
[3] => 5
[4] => 4
[5] => 15
[6] => 49
[7] => 14
[8] => 7
[9] => 2
[10] => 3
)
[yy] => Array
(
[0] => 1
[1] => 2
[2] => 1
[3] => 2
)
)
我想通过与xx数组比较来向yy数组插入空值,通过与较大的数组进行比较,我需要将空值插入到索引中的较小数组中。最后我需要像
这样的输出Array
(
[xx] => Array
(
[0] => 3
[1] => 10
[2] => 1
[3] => 5
[4] => 4
[5] => 15
[6] => 49
[7] => 14
[8] => 7
[9] => 2
[10] => 3
)
[yy] => Array
(
[0] => 1
[1] => 2
[2] => 1
[3] => 2
[4] => null
[5] => null
[6] => null
[7] => null
[8] => null
[9] => null
[10] => null
)
)
不仅是xx和yy。数组可以扩展。我们必须比较每个数组并在较小的数组中插入空值并与较大数组的大小匹配。最后每个数组计数应该相同
答案 0 :(得分:4)
假设你想
$array['yy'] = array_pad($array['yy'], count($array['xx']), null);
要均衡大量数组:
$length = max(array_map('count', $array));
$array = array_map(
function (array $a) use ($length) { return array_pad($a, $length, null); },
$array
);