开发者

merging two set of array values into one multidimesional array

开发者 https://www.devze.com 2022-12-21 08:38 出处:网络
i\'m a newbie in programming and in php too and i was wondering if anyone can help me with my array problem.

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


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


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号