2014-10-27 76 views
0

是否有任何方法來獲得兩個或更多數組的聯合?PHP陣列聯盟(線性產品)

例如:

$array1 = [x1, x2, x3]; 

$array2 = [y1, y2, y3]; 

// and the result will be 

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

是的,有很多方法可以做到這一點。你到目前爲止嘗試過什麼嗎? – 2014-10-27 10:58:15

+0

x1 y1是什麼意思?級聯?加成? – motanelu 2014-10-27 10:59:42

+1

它是什麼multiplicatrion – danielad 2014-10-27 11:00:44

回答

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

?> 
+0

這是幹什麼的? – danielad 2014-10-27 11:09:00

+0

@danielad它將兩個數組按照OP的要求組合在一起 – Rizier123 2014-10-27 11:34:25