PHP: 2 Boyutlu Dizileri Birleştirme

3 Cevap php

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.

Dizi # 1

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

Dizi # 2

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

Yeni Dizi

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"
        )
)

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++;
}

Bunu nasıl düzeltebilirim?

3 Cevap

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

[] yerine yazılmadan dizi için ekler.

Yerleşik dizi işlevlerden birini kullanın:

array_merge_recursive veya array_replace_recursive

http://php.net/manual/en/function.array-merge-recursive.php

Sadece döngüler ve dizi gösterimini kullanarak:

$newArray = array();
$i=0;
foreach($arary1 as $value){
  $newArray[$i] = $value;
  $newArray[$i][] = $array2["name"][$i];
  $i++;
}