Will
Will

Reputation: 47

Creating new two-dimensional PHP Array where keys match

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

Upvotes: 0

Views: 286

Answers (2)

Jarosław Gomułka
Jarosław Gomułka

Reputation: 4995

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

Upvotes: 3

user557846
user557846

Reputation:

$items=array();
foreach($array1 as $k=>$v){
$items[]=array($array1[$k],$array2[$k],$array3[$k]);
}

Upvotes: 0

Related Questions