php将两个数组与匹配的数组键组合在一起

时间:2014-07-09 12:15:24

标签: php arrays

我是PHP语言的新手,所以请尽量帮助我。现在我有两个数组,我想通过匹配它们的键将第二个数组与第一个数组组合。

谢谢:)

第一个数组如下所示:

Array
(
    [project_virtual_tour] => Array
        (
            [name] => 
            [type] => 
            [tmp_name] => 
            [error] => 4
            [size] => 0
        )

    [project_appartment_plans] => Array
        (
            [name] => 
            [type] => 
            [tmp_name] => 
            [error] => 4
            [size] => 0
        )

    [project_video] => Array
        (
            [name] => 
            [type] => 
            [tmp_name] => 
            [error] => 4
            [size] => 0
        )

    [project_photo] => Array
        (
            [name] => sample.png
            [type] => image/png
            [tmp_name] => /tmp/php32JYlt
            [error] => 0
            [size] => 3616
        )

    [project_aerial_view] => Array
        (
            [name] => 
            [type] => 
            [tmp_name] => 
            [error] => 4
            [size] => 0
        )

    [project_marker] => Array
        (
            [name] => 
            [type] => 
            [tmp_name] => 
            [error] => 4
            [size] => 0
        )

)

第二个数组如下所示:

Array
(
    [project_virtual_tour] => 14049071781773982829
    [project_appartment_plans] => 14049071781048072432
    [project_video] => 14049071781875011761
    [project_photo] => 14049071781835183205
    [project_aerial_view] => 14049071782034653259
    [project_marker] => 14049071781245067296
)

我想要这样的结果:

Array
(
    [project_virtual_tour] => Array
        (
            [name] => 
            [type] => 
            [tmp_name] => 
            [error] => 4
            [size] => 0
            [timestamp] => 14049071781773982829
        )

    [project_appartment_plans] => Array
        (
            [name] => 
            [type] => 
            [tmp_name] => 
            [error] => 4
            [size] => 0
            [timestamp] => 14049071781048072432
        )

    [project_video] => Array
        (
            [name] => 
            [type] => 
            [tmp_name] => 
            [error] => 4
            [size] => 0
            [timestamp] => 14049071781875011761
        )

    [project_photo] => Array
        (
            [name] => sample.png
            [type] => image/png
            [tmp_name] => /tmp/php32JYlt
            [error] => 0
            [size] => 3616
            [timestamp] => 14049071781835183205
        )

    [project_aerial_view] => Array
        (
            [name] => 
            [type] => 
            [tmp_name] => 
            [error] => 4
            [size] => 0
            [timestamp] => 14049071782034653259
        )

    [project_marker] => Array
        (
            [name] => 
            [type] => 
            [tmp_name] => 
            [error] => 4
            [size] => 0
            [timestamp] => 14049071781245067296
        )

)

2 个答案:

答案 0 :(得分:4)

简单的foreach循环可以解决这个问题:

foreach ($array2 as $key => $value) {
   if (isset($array1[$key]))
      $array1[$key]['timestamp'] = $value;
}

Barmar的评论也很准确。

答案 1 :(得分:0)

如果你可以得到第二个数组:

Array
(
    [project_virtual_tour] => Array([timestamp] => 14049071781773982829)
...

然后你可以做

foreach ($array as $key => &$value) {
    array_merge($value, $second[$key]);
}