在PHP中将两个数组混合在一起

时间:2015-02-28 10:48:30

标签: php arrays

假设我有两个这样的阵列:

$array1(one,  two, three);
$array2(four, five, six);

我希望结果如下:

[0] -> one four
[1] -> two five
[2] -> three six

我该怎么做?

2 个答案:

答案 0 :(得分:2)

您可以使用array_map函数在参数中提供两个数组:

<?php
$array1=Array("one",  "two", "three");
$array2=Array("four", "five", "six");

$res=array_map(function($r1, $r2) {return "$r1 $r2";}, $array1, $array2);
print_r($res);

结果

Array
(
    [0] => one four
    [1] => two five
    [2] => three six
)

答案 1 :(得分:1)

试试这个

function array_interlace() { 
$args = func_get_args(); 
$total = count($args); 

if($total < 2) { 
    return FALSE; 
} 

$i = 0; 
$j = 0; 
$arr = array(); 

foreach($args as $arg) { 
    foreach($arg as $v) { 
        $arr[$j] = $v; 
        $j += $total; 
    } 

    $i++; 
    $j = $i; 
} 

ksort($arr); 
return array_values($arr); 
}
$array1=array('one',  'two', 'three');
$array2=array('four', 'five',' six');
print_r(array_interlace($array1,$array2));