2017-04-03 53 views
0

我知道這可能是之前問過的,但不確定是否以這種形式存在,但我確實嘗試了一些重放,將一個子數組移動到父數組並更改父/子名

好,我有這樣的陣列

Array 
(
    [0] => Array 
     (
      [Data3] => Array 
       (
        [id] => 5 
        [category] => Whiskey 
        [name] => Some name 
        [description] => description 
        [image] => asdf.jpg 
        [price] => 83.99 
       ) 

      [ammount] => 1 
      [Data_id] => 3 
     ) 

    [1] => Array 
     (
      [Data3] => Array 
       (
        [id] => 4 
        [category] => Tequila 
        [name] => Something Red 75cl 
        [description] => description 
        [image] => sierratequilasilver100.jpg 
        [price] => 92.49 
       ) 

      [ammount] => 2 
      [Data_id] => 3 
     ) 

    [2] => Array 
     (
      [Data4] => Array 
       (
        [id] => 3 
        [category] => Whiskey 
        [name] => Some name Gold 
        [description] => description 
        [image] => asdf.jpg 
        [price] => 83.99 
       ) 

      [ammount] => 1 
      [Data_id] => 4 
     ) 

    [3] => Array 
     (
      [Data4] => Array 
       (
        [id] => 5 
        [category] => Vodka 
        [name] => Something Blue 100 cl 
        [description] => description 
        [image] => Something.jpg 
        [price] => 32.44 
       ) 

      [ammount] => 1 
      [Data_id] => 4 
     ) 

) 

我想什麼是結果是這樣的:

Array 
(
    [0] => Array 
     (
      [id] => 5 
      [category] => Whiskey 
      [name] => Some name 
      [description] => description 
      [image] => asdf.jpg 
      [price] => 83.99 
      [ammount] => 1 
      [Data_id] => 3 
     ) 

    [1] => Array 
     (
      [id] => 4 
      [category] => Tequila 
      [name] => Something Red 75cl 
      [description] => description 
      [image] => sierratequilasilver100.jpg 
      [price] => 92.49 
      [ammount] => 2 
      [Data_id] => 3 
     ) 
    [2] => Array 
     (
      [id] => 3 
      [category] => Whiskey 
      [name] => Some name Gold 
      [description] => description 
      [image] => asdf.jpg 
      [price] => 83.99 
      [ammount] => 1 
      [Data_id] => 4 
     ) 
    [3] => Array 
     (
      [id] => 5 
      [category] => Vodka 
      [name] => Something Blue 100 cl 
      [description] => description 
      [image] => Something.jpg 
      [price] => 32.44 
      [ammount] => 1 
      [Data_id] => 4 
     ) 
) 

或另一種方式,我可以一起工作是,如果我可以改變數據1 ,Data2,Data3等。

可以n數據取決於用戶選擇多少個產品

成同名EX簡單數據信息

例如:

Array 
(
    [0] => Array 
     (
      [Info] => Array 
       (
        [id] => 5 
        [category] => Whiskey 
        [name] => Some name 
        [description] => description 
        [image] => asdf.jpg 
        [price] => 83.99 
       ) 

      [ammount] => 1 
      [Data_id] => 3 
     ) 

    [1] => Array 
     (
      [Info] => Array 
       (
        [id] => 4 
        [category] => Tequila 
        [name] => Something Red 75cl 
        [description] => description 
        [image] => sierratequilasilver100.jpg 
        [price] => 92.49 
       ) 

      [ammount] => 2 
      [Data_id] => 3 
     ) 

任何解決方案將是對我很好。

感謝和問候

+2

爲什麼在那個結構中存儲的數據是以?開頭的?如果你正在構建陣列,那麼你可以在那時做它,因爲它會阻止你重建它 – atoms

+0

數據在那裏,因爲每個數組都是來自不同表的sql請求。 ex data3來自table3等等。 –

+0

然後您需要更改您的sql,以便它正確地檢索所有信息。看看SQL JOINS。如果任何答案在這裏解決您的問題,請標記爲正確,並在需要時打開一個新問題。 – atoms

回答

0

使用此代碼,你的結果:

$final_array = array(); 
foreach($array1 as $offset1 => $array2) { 

    $tmp_array = array(); 

    foreach($array2 as $offset2 => $array3) { 

     if(is_array($array3)) { 
      $tmp_array = $array3; 
     } else { 
      $tmp_array[$offset2] = $array3 
     } 

    } 

    $final_array = array_merge($final_array, $tmp_array;); 
    //or 
    $final_array[] = $tmp_array; 

} 
+0

這適用於。謝謝! –

0

您的第一選擇:

foreach($arr as $key => $value){ 
     foreach($value as $k => $val){ 
      if(is_array($val)){      
       $arr[$key] = $val; 
       unset($arr[$key][$k]); 
      } 
     } 
    } 
    echo "<pre>"; print_r($arr); 

Check output here

+0

例如,但經過一些測試後,它不顯示我的金額和data_id以獲取更多值。 –

0

我會做這樣這樣。不過,我會試着解決爲什麼數據是以這種結構開始的。

$aStartArray = array(array('Data3'=>array('id'=>1, 'cat'=>2), 'amount' => 1, 'Data_id'=>3)); 

foreach ($aStartArray as $iPos => $aArray) { 

    $aKeys = array_keys($aArray); // fetches all the keys 
    $aFirstElement = $aArray[$aKeys[0]]; // Get the first element using first key 

    // assign/ overwrite data at the same position 
    $aStartArray[$iPos] = array($aFirstElement, 'amount' => $aArray['amount'], 'Data_id' => $aArray['Data_id']); 
} 

echo "<pre>"; 
var_dump($aStartArray); 
+0

這個工作在我的例子中,但我的變量是動態的。謝謝! –

相關問題