I was looking for a function that could combine an array to multiple one, for my MySQL GROUP_CONCAT() query, so I made this function.
<?php
function array_combine_array(array $keys)
{
$arrays = func_get_args();
$keys = array_shift($arrays);
$check = count(array_unique(array_map('is_array',array_map('current',$arrays)))) === 1;
if (!$check) { trigger_error('Function array_combine_array() expects all parameters to be same type array or array of array',E_USER_NOTICE); return array(); }
$assocArray = is_array(array_shift(array_map('current',$arrays)));
if (empty($keys)) $keys = array_keys(array_fill(0,max(($assocArray) ? array_map('count',array_map('current',$arrays)) : array_map('count',$arrays)),'foo'));
$ret=array();$i=0;
foreach($keys as $v)
{
foreach ($arrays as $k)
{
if ($assocArray)
{
$key = key($k);
$ret[$v][$key] = isset($k[$key][$i]) ? $k[$key][$i]:false;
}
else
$ret[$v][] = isset($k[$i]) ? $k[$i]: false;
}
$i++;
}
return $ret;
}
$r = array(1,2,4,10);
$a1 = array('one','two','four','ten');
$a2 = array('un','deux','quatre','dix');
$a3 = array('uno','dos','quatro','diez');
print_r(array_combine_array($r,array('english' => $a1),array('french' => $a2),array('spanish' => $a3))); print_r(array_combine_array($r,$a1,array('french' => $a2),array('spanish' => $a3))); print_r(array_combine_array($r,$a1,$a2,$a3)); print_r(array_combine_array(array(),array('english' => $a1),array('french' => $a2),array('spanish' => $a3))); ?>