tags:

views:

72

answers:

2

hi all, i'm a newbie in programming and in php too and i was wondering if anyone can help me with my array problem.

i have two set of array, example:

$name = array("peter","peter","joe");  
$cars = array("ford", "gmc", "mercy");  

and i would like to merge them into a multidimensional array like this

$merge = array(array($name[0], $cars[0]),array($name[1], $cars[1]),array($name[2], $cars[2]));

now, i would like keep the structure as above but i would like to do it with a native array function or foreach function.

i've tried array_merge and array_combine but it didn't turn out as i expected.
i've tried $arr3 = $name + $cars; but it didn't work too

does anyone can help me on what function should i use?

many thanks
~aji

+4  A: 

array_map sounds like what you are looking for. See "Example #4 Creating an array of arrays"

An interesting use of this function is to construct an array of arrays, which can be easily performed by using NULL as the name of the callback function

$merged = array_map(NULL, $name, $cars);
jasonbar
nice and simple.
Gordon
fantastic, works like a charm.many thanks jasonbar
aji
A: 
$name = array("peter","peter","joe");
$cars = array("ford", 'gm$c', "mercy");
for($i=0;$i<count($name);$i++){
  $array[$i]=array($name[$i],$cars[$i]);
}
print_r($array);
ghostdog74