Reputation: 71
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
Upvotes: 2
Views: 767
Reputation: 342313
$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);
Upvotes: 0
Reputation: 13461
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);
Upvotes: 5