PHP数组联合(线性产品)

时间:2014-10-27 10:55:02

标签: php arrays

是否有任何方法可以获得两个或更多数组的并集?

示例:

$array1 = [x1, x2, x3];

$array2 = [y1, y2, y3];

// and the result will be 

$array2 = [x1 y1, x1 y2, x1 y3, x2 y1, x2 y2 ,.....];

1 个答案:

答案 0 :(得分:2)

这应该适合你:

<?php 

    $array1 = array("x1", "x2", "x3"
    $array2 = array("y1", "y2", "y3");

    $array3 = array();

    // and the result will be   
    //$array3 = [x1 y1, x1 y2, x1 y3, x2 y1, x2 y2 ,.....];


    for($count = 0; $count < count($array1); $count++) {

        for($countArrayTwo = 0; $countArrayTwo < count($array2); $countArrayTwo++)
            $array3[]  = $array1[$count] . " " . $array2[$countArrayTwo];

    }

    print_r($array3);

?>