我现在正努力将这两个数组组合在一起。有人可以帮我使用什么吗?
Array
(
[1] => Array
(
[0] => 1-1
[1] => 1-2
[2] => 1-1
[3] => 1-2
[4] => 1-1
)
[3] => Array
(
[0] => 3-3
[1] => 3-3
[2] => 3-4
[3] => 3-4
[4] => 3-3
)
)
Array[1] key [0] => 1-1 needs to combine with
Array[3] key [0] => 3-3
Array[1] key [1] => 1-2 needs to combine with
Array[3] key [1] => 3-3
结果将是:1-1,3-3和1-2,3-3
请注意,第一个数组[1]和[3]中的键可以是动态的。
我这样做:
print_r(array_merge_recursive($optionWithValue[1], $optionWithValue[3]));
但是现在我已经对1和3进行了硬编码,并且可以更改,最后我得到:
Array
(
[0] => 1-1
[1] => 1-2
[2] => 1-1
[3] => 1-2
[4] => 1-1
[5] => 3-3
[6] => 3-3
[7] => 3-4
[8] => 3-4
[9] => 3-3
)
那也不是我所需要的
答案 0 :(得分:1)
如果数组中数组的键相同,则可以使用array_reduce:
$arrays = [
[
"1-1",
"1-2",
"1-1",
"1-2",
"1-1",
],
[
"3-3",
"3-3",
"3-4",
"3-4",
"3-3",
],
];
$first = array_shift($arrays);
$res = array_reduce($arrays, function($carry, $item){
foreach($item as $key => $value) {
$carry[$key] = $carry[$key] . "," . $value;
}
return $carry;
}, $first);
print_r($res);
结果
Array
(
[0] => 1-1,3-3
[1] => 1-2,3-3
[2] => 1-1,3-4
[3] => 1-2,3-4
[4] => 1-1,3-3
)