2016-11-24 52 views
0

我有這樣https://3v4l.org/VrIDe從陣列

$arr = array(
'slidelink' => 'presentation.pptx', 
'productid' => array(1,3), 
'order' => 2, 
'class_id' => array(1,2), 
'currency_id' => array(1,2), 
'presentation_type' => 1, 
'distribution' => 0, 

)的陣列生成所有可能的變型;

我想生成的所有可能輸出到一個新的數組,所以在這種情況下,8行,如:

$new_arr = array (
0 => array(presentation.pptx, 1, 2, 1, 1, 1, 0), 
1 => array(presentation.pptx, 1, 2, 1, 2, 1, 0), 
2 => array(presentation.pptx, 1, 2, 2, 1, 1, 0), 
3 => array(presentation.pptx, 1, 2, 2, 2, 1, 0), 
4 => array(presentation.pptx, 3, 2, 1, 1, 1, 0), 
5 => array(presentation.pptx, 3, 2, 1, 2, 1, 0), 
6 => array(presentation.pptx, 3, 2, 2, 1, 1, 0), 
7 => array(presentation.pptx, 3, 2, 2, 2, 1, 0) 
); 

我該怎麼辦呢?

謝謝!

+0

你到目前爲止嘗試過什麼? – Dekel

回答

1

對所有數組元素使用嵌套循環。

要處理元素可能不是數組的可能性,可以使用強制轉換將其轉換爲數組。

$new_arr = array(); 
foreach ((array)$arr['slidelink'] as $s) { 
    foreach ((array)$arr['productid'] as $pid) { 
     foreach ((array)$arr['order'] as $o) { 
      foreach ((array)$arr['class_id'] as $cid) { 
       foreach ((array)$arr['currency_id'] as $curr) { 
        foreach ((array)$arr['presentation_type'] as $p) { 
         foreach ((array)$arr['distribution'] as $d) { 
          $new_arr[] = array($s, $pid, $o, $cid, $curr, $p, $d); 
         } 
        } 
       } 
      } 
     } 
    } 
} 
+0

謝謝,它有正確的輸出,但僅限於提供的陣列。這個數組每次都會有所不同,例如product_id可能是單個值,但分佈變成一個數組(1,2)。 – giker

+0

這是一個可怕的設計。你應該對每個元素是使用數組還是單值都一致。 – Barmar

+0

但是這個數組是excel行分解的輸出,如'3','presentation.pptx','1','2','1; 2','1; 2','1', '0' – giker