2017-08-16 149 views
0

我嘗試從複雜數組中創建一個新數組。 如果沒有簡單的解決方案,每個提示將有助於搜索更成功。PHP:使用值作爲鍵創建多維數組中的二維數組

我有這種複雜的陣列(縮短很多),並希望建立一個新的2維陣列:

array (
    [key1] => value1 
    [key2] => value2 
    [category] => array (
     [key3] => value3 
     [key4] => array (
      [small] => value6 
      [large] => value7 
      ) 
     [items] => array (
      [0] => array (
       [aTag] => #PU2RRL 
       [name] => 3PL 
       [position] => 25 
       [versions] => array (
        [0] => array (
         [bTag] => #KF67RL 
         [color] => blue 
         [id] => 001 
         ) 
        [1] => array (
         [bTag] => #Z8TR4 
         [color] => red 
         [id] => 002 
         ) 
       ) 
      ) 
      [1] => array (
       ... 

這是我要創建的數組:

array(
    [001] => array (
    [aTag] => #PU2RRL 
    [name] => 3PL 
    [position] => 25 
    [bTag] => #KF67RL 
    [color] => blue 
) 
    [002] => array (
    [aTag] => #PU2RRL 
    [name] => 3PL 
    [position] => 25 
    [bTag] => #Z8TR4 
    [color] => blue)) 

隨着ID作爲鍵和這個值:

$itemAll = array(
    $array[category][items][0][versions][0][id] => array(
    "aTag" => $array[category][items][0][aTag], 
    "name" => $array[category][items][0][name], 
    "position" => $array[category][items][0][position], 
    "bTag" => $array[category][items][0][versions][0][bTag], 
    "color" => $array[category][items][0][versions][0][color], 
) 
); 

我不知道如何創建這個數組與foreach循環的「項目」和ID作爲主鍵的版本,任何提示?


編輯:非常感謝@DeeDuu!因爲我有多個項目,我添加了另一個foreach:

$new_array = array(); 
// i have multiple items, so I removed [0]: 
$items = $array["category"]["items"]; 
// added another foreach 
foreach ($items as $item) { 
    // changed $items to $item 
    $shared_properties = array(
    "aTag" => $item["aTag"], 
    "name" => $item["name"], 
    "position" => $item["position"] 
); 
    // changed $items to $item 
    foreach ($item["versions"] as $version) { 
    $specific_properties = array(
     "bTag" => $version["bTag"], 
     "color" => $version["color"] 
); 
    $new_entry = array_merge(
     $shared_properties, 
     $specific_properties 
); 
    $new_array[$version["id"]] = $new_entry; }} 

回答

0

如果我理解正確,你想要什麼,像下面的東西應該工作。

// This is the target array where we will save the recombinated data 
$new_array = array(); 

// Store the relevant subarray in a new variable; 
// This makes the subsequent code shorter 
$items = $array["category"]["items"][0]; 

// This gives the data that will be common to all the new entries, 
// for all versions seem to share aTag, name and position 
$shared_properties = array(
    "aTag" => $items["aTag"], 
    "name" => $items["name"], 
    "position" => $items["position"] 
); 

// Alright, let's loop over the versions 
foreach ($items["versions"] as $version) { 
    // Another array for the data that is specific 
    // to the version we're currently looking at 
    $specific_properties = array(
     "bTag" => $version["bTag"], 
     "color" => $version["color"] 
    ); 

    // The new entry is a combination of the shared and the 
    // specific properties; array_merge will do that for us 
    $new_entry = array_merge(
     $shared_properties, 
     $specific_properties 
    ); 

    // Now add to the new array 
    $new_array[$version["id"]] = $new_entry; 
} 
+0

對不起,錯過了通知,巨大的感謝,工作perfect.I有多個項目,所以我加了另一個的foreach – LeoRon

+0

@LeoRon很高興我能幫助你!由於這解決了您的問題,請投票並接受答案。 – 2017-08-18 16:14:57

+0

已經接受並投票(我的第一個問題在這裏,仍然在學習),我的投票雖然不會顯示出來,只有不到15的聲望 – LeoRon