[英]how to merge two arrays in codeigniter
i want to merge two array, but how? 我想合并两个数组,但是怎么样?
$array1 [ ] = Array ( [0] => Array ( [id] => 1 [name] => "Alice" ))
$array2 [] = Array ( [0] => Array ( [age] => 22 ));
The result array i want to get: 我想得到的结果数组:
$result = Array ( [0] => Array ( [id] => 1 [name] => "Alice" [age] => 22) );
anyone could help? 谁有人可以帮忙?
您可以使用PHP的array_merge 。
你可以做到
array_merge_recursive($array1, $array2);
if this is only single array entry then you can try this otherwise you have to apply loop for complete set of arrays 如果这只是单个数组条目,那么你可以试试这个,否则你必须为完整的数组集应用循环
$result[] = $array1[0] + $array2[0];
// print_r($result ); // print_r($ result);
it will give you output 它会给你输出
Array
(
[0] => Array
(
[id] => 1
[name] => Alice
[age] => 1
)
)
Iter through array1. Iter通过array1。 If the first Element is always an array, just add the elements of array2[c] to the array array1[c], where c is the counting variable.
如果第一个Element总是一个数组,只需将array2 [c]的元素添加到数组array1 [c]中,其中c是计数变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.