2016-02-29 277 views
1

我試圖從下面的環PHP創建動態陣列

foreach($all_arrays as $option => $all_arrays_vals){ 

    foreach($all_arrays_vals as $all_arrays_vals_key => $value){ 

      //Dynamic arrays goes here 

     print 'options = '.$option .' values = '.$value.'<br />'; 

     } 
    } 

上述代碼生成輸出

options = Type values = GOLD 
options = Purity values = 75.0+% 
options = Purity values = 58.33+% 
options = Purity values = 37.5k 
options = Model values = Quantity 
options = Model values = 1.0mm 
options = Model values = 1.2mm 

I need every same "option" an array, e.g 

type = {GOLD}; 

purity = {75.0+% , 58.33+% , 37.5k}; 

Model = {Quantity , 1.0mm , 1.2mm}; 

在該特定示例做出動態數組它有三個「選項」(類型,純度,模型),我想創建數組。每次運行這些循環時,可能會有不同數量的數組(OPTIONS)。 希望我已經說明了問題

感謝

回答

0

保存在一個新的array這些項目,同時檢查是否index已經set

$array = array();   

    foreach($all_arrays as $option => $all_arrays_vals){ 

     foreach($all_arrays_vals as $all_arrays_vals_key => $value){ 

      if(!isset($array[$option])){ 

       $array[$option] = array(); 

      } 

      $array[$option][] = $value; 

     } 

    } 

    var_dump($array);