views:

290

answers:

2

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.

Array #1

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

Array #2

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

New Array

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

How do I correct this?

+2  A: 
$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.

Jay Paroline
Thanks, i was so close :D
ticallian
+1  A: 

Using just loops and array notation:

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