0

I have the following three arrays and need to create a new two-dimensional array where the keys match.

Array
(
    [0] => Item 0
    [1] => Item 1
    [2] => Item 2
    [3] => Item 3
Array
(
    [0] => £35.00
    [1] => £60.00
    [2] => £24.00
    [3] => £79.00
)
Array
(
    [0] => 2
    [1] => 1
    [2] => 1
    [3] => 1
)

I need my new array as follows:

$items = Array( 
           Array("Item 0", "£35.00" , 2),
           Array("Item 1", "£60.00" , 1),
           Array("Item 2", "£24.00" , 1),
           Array("Item 3", "£79.00" , 1)
         );

I've tried using array_merge, array_merge_recursive, array_combine, $array1+$array2+$array3 but none of them seem to do what I'm after.

Any pointers would be appreciated :) Many thanks

Will
  • 47
  • 4
  • 1
    possible duplicate of [Joining Arrays in PHP](http://stackoverflow.com/questions/1962933/joining-arrays-in-php) – hakre Apr 06 '12 at 09:20

2 Answers2

3

As long as all the arrays are the same length, you can use array_map­Docs with null as callback

print_r(array_map(null,
    $array1, $array2, $array3 
));
hakre
  • 193,403
  • 52
  • 435
  • 836
Jarosław Gomułka
  • 4,935
  • 18
  • 24
0
$items=array();
foreach($array1 as $k=>$v){
$items[]=array($array1[$k],$array2[$k],$array3[$k]);
}
  • Thanks, this did the trick and was easiest for me to understand :) I'll mark as solved when it lets me in a few minutes. Cheers – Will Apr 06 '12 at 09:05