如何根据值合并两个muti维数组?

时间:2021-10-24 01:44:24

Hi, I have two arrays like this

你好,我有两个这样的数组

array (
[0]=>array(
     [0]=>10,
     [1]=>Some Name..
),
[1]=>array(
     [0]=>11,
     [1]=>Some Name..    
),
[2]=>array(
     [0]=>13,
     [1]=>Some Name..    
 )
)

Another array like this

这样的另一个数组

array (
[0]=>array(
     [0]=>13,
     [1]=>Viewed
    )
)

How can I merge above two arrays without using any looping? Any php functionality is available for this? I need this kind of an out put

如何在不使用任何循环的情况下合并上述两个数组?这里有什么php功能吗?我需要这样一个输出

array (
     [0]=>array(
              [0]=>10,
              [1]=>Some Name..
     ),
     [1]=>array(
              [0]=>11,
              [1]=>Some Name..
     ), [2]=>array(
              [0]=>13,
              [1]=>Some Name..
              [2]=Viewed
     )
)

2 个解决方案

#1


6  

You can use the PHP function array_merge_recursive. See the example:

可以使用PHP函数array_merge_recursive。看这个例子:

<?php
$ar1 = array("color" => array("favorite" => "red"), 5);
$ar2 = array(10, "color" => array("favorite" => "green", "blue"));
$result = array_merge_recursive($ar1, $ar2);
print_r($result);
?>

#2


0  

<?php
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
?>

For more detail Check :

详细检查:

http://php.net/manual/en/function.array-combine.php

http://php.net/manual/en/function.array-combine.php

http://php.net/manual/en/function.array-merge.php

http://php.net/manual/en/function.array-merge.php

See Also

另请参阅

array array_merge ( array $array1 [, array $... ] ) array_merge_recursive() - Merge two or more arrays recursively array_combine() - Creates an array by using one array for keys and another for its values array operators

数组array_merge(数组$array1[,数组$…array_merge_recursive()——递归地合并两个或多个数组array_combine()——通过使用一个数组作为键,另一个数组作为值数组操作符来创建数组

#1


6  

You can use the PHP function array_merge_recursive. See the example:

可以使用PHP函数array_merge_recursive。看这个例子:

<?php
$ar1 = array("color" => array("favorite" => "red"), 5);
$ar2 = array(10, "color" => array("favorite" => "green", "blue"));
$result = array_merge_recursive($ar1, $ar2);
print_r($result);
?>

#2


0  

<?php
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
?>

For more detail Check :

详细检查:

http://php.net/manual/en/function.array-combine.php

http://php.net/manual/en/function.array-combine.php

http://php.net/manual/en/function.array-merge.php

http://php.net/manual/en/function.array-merge.php

See Also

另请参阅

array array_merge ( array $array1 [, array $... ] ) array_merge_recursive() - Merge two or more arrays recursively array_combine() - Creates an array by using one array for keys and another for its values array operators

数组array_merge(数组$array1[,数组$…array_merge_recursive()——递归地合并两个或多个数组array_combine()——通过使用一个数组作为键,另一个数组作为值数组操作符来创建数组