PHP数组联合(线性产品)

PHP array union (linear product)

有没有任何方法可以得到两个或多个数组的并集?

例子:

1
2
3
4
5
6
7
$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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?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);

?>