按索引合并2个数组

时间:2015-02-25 19:39:18

标签: php

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

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

3 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

必填:

  • 对于' array2'中的每个元素(雅虎,com)
  • 获取相应的条目'来自' array1 - 这是一个数组
  • 将它们合并然后
  • 输出到结果数组。

完整测试代码: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 - viper 7'不像我想的那么可靠。我希望通过他们的网站提供这个答案的完整源代码以及预期的结果。

以下是我使用的完整代码:

<?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 />';

}

答案 2 :(得分:0)

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

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