PHP:合并 2 个多维数组 [英] PHP: Merge 2 Multidimensional Arrays

查看:43
本文介绍了PHP:合并 2 个多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将 2 个多维数组合并在一起以创建一个新数组.
这 2 个数组是从 $_POST$_FILES 创建的,我需要它们相互关联.

I need to merge 2 multidimensional arrays together to create a new array.
The 2 arrays are created from $_POST and $_FILES and I need them to be associated with each other.

数组#1

Array 
(
    [0] => Array
        (
            [0] => 123  
            [1] => "Title #1"
            [2] => "Name #1"
        )
    [1] => Array
        (
            [0] => 124  
            [1] => "Title #2"
            [2] => "Name #2"
        )
)

数组#2

Array
(
    [name] => Array
        (
            [0] => Image001.jpg
            [1] => Image002.jpg
        )
)

新数组

Array
(
    [0] => Array
        (
            [0] => 123  
            [1] => "Title #1"
            [2] => "Name #1"
            [3] => "Image001.jpg"
        )
    [1] => Array
        (
            [0] => 124  
            [1] => "Title #2"
            [2] => "Name #2"
            [3] => "Image002.jpg"
        )
)

我正在使用的当前代码有效,但仅适用于数组中的最后一项.
我假设通过循环 array_merge 函数,它会在每个循环中擦除我的新数组.

The current code i'm using works, but only for the last item in the array.
I'm presuming by looping the array_merge function it wipes my new array every loop.

$i=0;
$NewArray = array();
foreach($OriginalArray as $value) {
    $NewArray = array_merge($value,array($_FILES['Upload']['name'][$i]));
    $i++;
}

我该如何纠正?

推荐答案

$i=0;
$NewArray = array();
foreach($OriginalArray as $value) {
    $NewArray[] = array_merge($value,array($_FILES['Upload']['name'][$i]));
    $i++;
}

[] 会将它附加到数组中而不是覆盖.

the [] will append it to the array instead of overwriting.

这篇关于PHP:合并 2 个多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆