Php三阵列合并组合

时间:2015-12-23 14:22:16

标签: php arrays

$arr1 = array(1,2,3); 
$arr2 = array("a","b","c"); 
$arr3 =array("1a","2b","3c");

我该怎样做?

打印

$one = 1,a,1a
$two = 2,b,2b
$three = 3,c,3c

3 个答案:

答案 0 :(得分:3)

使用array_map()函数同时映射所有数组。像这样:

getBoxBackgroundColor

答案 1 :(得分:1)

你可以试试这个:

$arr1 = array(1,2,3); 
$arr2 = array("a","b","c"); 
$arr3 = array("1a","2b","3c");

$i = 0;
foreach ($arr1 as $key => $value) {
    $newArr[] = $value.",".$arr2[$i].",".$arr3[$i];
$i++;
}
echo implode("<br/>", $newArr);

结果:

1,a,1a
2,b,2b
3,c,3c

您也可以使用for loop执行此操作。

答案 2 :(得分:0)

试试这个:

$arr1 = [1,2,3]; 
$arr2 = ["a","b","c"]; 
$arr3 = ["1a","2b","3c"];

$matrix = [$arr1, $arr2, $arr3];

var_dump(transpose($matrix));

function transpose($matrix) {

    return array_reduce($matrix, function($carry, $item) {
        array_walk($item, function ($value, $key) use (&$carry) {
            $carry[$key][] = $value;
        });
        return $carry;
    });
}