繁体   English   中英

按索引合并2个数组

[英]Merge 2 arrays by index

我正在尝试通过索引合并以下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          
)    
)

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

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

}

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM