[英]How to collect 3 arrays in 1 array using PHP?
I have 3 arrays: 我有3个数组:
Array
(
[0] => l_ka3a1
[1] => l_ka3a2
[2] => l_ka3a3
)
Array
(
[0] => l_no_inspection
[1] => l_elbab_alresi
[2] => l_bab_alqa3a
)
Array
(
[0] => notes
[1] => notes
[2] => null
)
I want to collect them in 1 array: 我想将它们收集在1个数组中:
Array(
Array
(
[0] => l_ka3a1
[1] => l_no_inspection
[2] => notes
)
Array
(
[0] => l_ka3a2
[1] => l_elbab_alresi
[2] => notes
)
Array
(
[0] => l_ka3a3
[1] => l_bab_alqa3a
[2] => null
)
)
witch every index in old arrays with the same one in another and collect them in 1 array. 将旧数组中的每个索引与另一个索引中的每个索引并以1个数组进行收集。
Use array_map()
to creating new array contain target three arrays. 使用
array_map()
创建包含目标三个数组的新数组。
$newArr = array_map(function($v1, $v2, $v3){
return [$v, $v2, $v3];
}, $arr1, $arr2, $arr3);
One of many ways using simple for
loop, 使用简单的
for
循环的多种方法之一 ,
$expected = [];
for($i=0;$i<3;$i++){
$expected[] = [$arr1[$i],$arr2[$i],$arr3[$i]];
}
print_r($expected);
DEMO: https://3v4l.org/9Oo5D 演示: https : //3v4l.org/9Oo5D
I don't know which one do you like. 我不知道你喜欢哪一个。 But for me, I will do like this.
但是对我来说,我会这样做。
$array = [$arr1, $arr2, $arr3];
OR I will make it more dynamic using array_push() 或者我将使用array_push()使它更具动态性
$array = [];
array_push($array, $arr1);
// Some code maybe
array_push($array, $arr2);
// Some code maybe
array_push($array, $arr3);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.