Reputation: 1694
I have 2 multidimentional arrays:
array1[0][0] = 'yes';
array1[0][1] = 'no';
array1[1][0] = 'gilbert';
array1[1][1] = 'duncan';
and
array2[0][0] = 'good';
array2[0][1] = 'bad';
array2[1][0] = 'vegetables';
array2[1][1] = 'chocolate';
I want to create a new array from this 2 arrays such that:
array3[0][0] = 'yes';
array3[0][1] = 'no';
array3[0][2] = 'good';
array3[0][3] = 'bad';
array3[1][0] = 'gilbert';
array3[1][1] = 'duncan';
array3[1][2] = 'vegetables';
array3[1][3] = 'chocolate';
I am using PHP. How do I achieve this. PHP array_merge()
appends one array to another. What I want is to create a new array with the two arrays side by side.
Upvotes: 0
Views: 346
Reputation: 13435
You'll need to just walk them. You've defined a fairly specific (and interesting) way of merging them. I'd probably recommend a function like this:
function my_combine_arrays()
{
// This lets us accept N arrays as an input.
$arrList = func_get_args();
$retval = array();
foreach ($arrList as $array)
{
// Note the $key here. If the arrays have different key types
// like an assoc, you may have issues.
foreach ($array as $key=>$arrsub)
{
foreach ($arrsub as $arritem)
{
$retval[$key][] = $arritem;
}
}
}
return $retval;
}
Upvotes: 3