How to merge two muti dimensional array according to the values?

不问归期 提交于 2019-12-30 11:11:14

问题


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

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

回答1:


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

<?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:


<?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-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



来源:https://stackoverflow.com/questions/8879767/how-to-merge-two-muti-dimensional-array-according-to-the-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!