在php中合并多维数组

时间:2016-02-16 16:05:18

标签: php arrays multidimensional-array

我尝试将多维数组合并为一个。我正在使用array_merge函数,但它不起作用。

看看数组

$arr1 = [['title' => 'first title', 'phone' => '9923490', 'address' => 'thes 38'], ['title' => 'second title', 'phone' => '42342', 'address' => '2th thes 38 aradippou']];
$arr2 = [['town' => 'london'], ['town' => 'manchester']];


Array
(
    [0] => Array
        (
            [title] => first title
            [phone] => 9923490
            [address] => thes 38 aradippou
        )

    [1] => Array
        (
            [title] => second title
            [phone] => 42342
            [address] => 2th thes 38 aradippou
        )

)
Array
(
    [0] => Array
        (
            [town] => london
        )

    [1] => Array
        (
            [town] => manchester
        )

)

我想将城镇字段添加到$ arr1,所以它看起来像下面的数组:

Array
(
    [0] => Array
        (
            [title] => first title
            [phone] => 9923490
            [address] => thes 38 aradippou
            [town] => london
        )

    [1] => Array
        (
            [title] => second title
            [phone] => 42342
            [address] => 2th thes 38 aradippou
            [town] => manchester
        )

)

我尝试了array_merge功能但结果如下:

Array
(
    [0] => Array
        (
            [title] => first title
            [phone] => 9923490
            [address] => thes 38 aradippou
        )

    [1] => Array
        (
            [title] => second title
            [phone] => 42342
            [address] => 2th thes 38 aradippou
        )

    [2] => Array
        (
            [town] => london
        )

    [3] => Array
        (
            [town] => manchester
        )

)

3 个答案:

答案 0 :(得分:2)

您可以使用union operator

来完成此操作
$newArray = array();
  for($i=0;$i<count($arr1);$i++){
  $newArray[] = $arr1[$i]+$arr2[$i];
}
print_r($newArray);

与foreach

$newArray = array();
  foreach($arr1 as $k=>$val){
  $newArray[] = $arr1[$k]+$arr2[$k];
}
print_r($newArray);

这将为您提供所需的结果:

Array
(
    [0] => Array
        (
            [town] => london
            [title] => first title
            [phone] => 9923490
            [address] => thes 38
        )

    [1] => Array
        (
            [town] => manchester
            [title] => second title
            [phone] => 42342
            [address] => 2th thes 38 aradippou
        )

)

答案 1 :(得分:-1)

您可以使用array_merge,如下所示:

$out = array();
foreach ($arr1 as $key => $value){
    $out[] = array_merge($arr2[$key], $value);
}
print_r($out);

输出:

Array
(
    [0] => Array
        (
            [town] => london
            [title] => first title
            [phone] => 9923490
            [address] => thes 38
        )

    [1] => Array
        (
            [town] => manchester
            [title] => second title
            [phone] => 42342
            [address] => 2th thes 38 aradippou
        )

)

答案 2 :(得分:-1)

这个功能可以做你想要的......

def user_select():  # your function
    #......your code goes here
    return level

level = user_select() # the level variable within your function is now in this variable