本文介紹了PHP:合并 2 個多維數(shù)組的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
我需要將 2 個多維數(shù)組合并在一起以創(chuàng)建一個新數(shù)組.
這 2 個數(shù)組是從 $_POST
和 $_FILES
創(chuàng)建的,我需要它們相互關(guān)聯(lián).
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.
數(shù)組#1
Array
(
[0] => Array
(
[0] => 123
[1] => "Title #1"
[2] => "Name #1"
)
[1] => Array
(
[0] => 124
[1] => "Title #2"
[2] => "Name #2"
)
)
數(shù)組#2
Array
(
[name] => Array
(
[0] => Image001.jpg
[1] => Image002.jpg
)
)
新數(shù)組
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"
)
)
我正在使用的當(dāng)前代碼有效,但僅適用于數(shù)組中的最后一項.
我假設(shè)通過循環(huán) array_merge
函數(shù),它會在每個循環(huán)中擦除我的新數(shù)組.
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++;
}
[] 會將它附加到數(shù)組中而不是覆蓋.
the [] will append it to the array instead of overwriting.
這篇關(guān)于PHP:合并 2 個多維數(shù)組的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!