按索引合并2个数组


Merge 2 arrays by index

我正试图通过索引合并以下两个数组:

$array1 = array(array(77, 87), array(23, 45));
$array2 = array("yahoo", "com");

输出应该完全像这样:

Array  
(      
[0] => Array          
(              
[0] => yahoo              
[1] => 77              
[2] => 87          
)        
[1] => Array          
(              
[0] => com              
[1] => 23              
[2] => 45          
)    
)

可能有更好的解决方案,但这应该有效:

function mergeArrays($array1, $array2)
{
    $newArray = array();
    $newArrayIndex = 0;
    foreach($array1 as $value)
    {
        $newArray[$newArrayIndex] = array();
        if(is_array($value))
        {
            foreach($value as $value2)
            {
                $newArray[$newArrayIndex][] = $value2;
            }
        }
        else
        {
            $newArray[$newArrayIndex][] = $value;
        }
        $newArrayIndex++;
    }
    $newArrayIndex = 0;
    foreach($array2 as $value)
    {
        if(is_array($value))
        {
            foreach($value as $value2)
            {
                $newArray[$newArrayIndex][] = $value2;
            }
        }
        else
        {
            $newArray[$newArrayIndex][] = $value;
        }
        $newArrayIndex++;
    }
    return $newArray;
}
$array1 = array(array(77, 87), array(23, 45));
$array2 = array("yahoo", "com");
print_r(mergeArrays($array2, $array1));

必需:

  • 对于"array2"(yahoo,com)中的每个元素
  • 从数组"array1"中获取相应的"entry"
  • 合并它们,然后
  • 输出到结果数组

完整测试代码:Codepad.org

代码:

/**
 * Drive off array2 and merge corresponding entry from array1
 */
$result = array();
// drive off 'array2'...
while (current($array2)) { // use the array iterator
    $combined = array(); // store the current merged array in here
    $combined[] = current($array2);
    $idxArray2 = key($array2);
    foreach($array1[$idxArray2] as $value) { // append the associated entries
        $combined[] = $value;
    }
    // now we have the required complete entry - add it to the output.
    $result[] = $combined;
    next($array2);
}

遗憾的是,看起来"codepen-viper7"并不像我想象的那么可靠。我希望这个答案的完整源代码可以通过他们的网站以及预期的结果获得。

这是我使用的完整代码:

<?php // https://stackoverflow.com/questions/28728084/merge-2-arrays-by-index
/**
 * Driving table
 */
$array2 = array("yahoo", "com");
/**
 * Asume that corresponding index in 'Driving' table matches with this table
 *
 * i.e. 'yahoo' => index 0 matches this: array1[0]
 *
 *      'com'  =>  index 1 matches this: array1[1]
 */
$array1 = array( array(77, 87),
                  array(23, 45));
/**
 * required output
 */
$reqd = Array(
    0 => Array(
        0 => 'yahoo',
        1 => 77,
        2 => 87
        ),
    1 => Array(
         0 => 'com',
         1 => 23,
         2 => 45
    ),
);
/**
 * now we need an 'understandable' method that gives us a useful result...
 */
$result = array();
// drive off 'array2'...
while (current($array2)) { // use the array iterator
    $combined = array(); // store the current merged array in here
    $combined[] = current($array2);
    $idxArray2 = key($array2);
    foreach($array1[$idxArray2] as $value) { // append the associated entries
        $combined[] = $value;
    }
    // now we have the required complete entry - add it to the output.
    $result[] = $combined;
    next($array2);
}
/**
 * show the required input and the actual output
 */
echo '<br />required:<pre>';
print_r($reqd);
echo '</pre><br />';
echo '<br />actual:<pre>';
print_r($result);
echo '</pre><br />';
if (serialize($reqd) == serialize($result)) {
    echo 'all ok';
}
else {
    echo 'no match<br />';
}

我认为更简单的解决方案:

// merge arrays
$array1 = array(array(77, 87), array(23, 45));
$array2 = array("yahoo", "com");
// combine array so $array2 act as a "key" array, and array1 as values
$combined = array_combine($array2, $array1);
// merge key and values
foreach ($combined as $k => $v) {
    $combined[$k] = array_merge(array($k), $v);
}
// desired output
var_dump(array_values($combined));