2017-08-16 14 views
0

複雑な配列から新しい配列を作成しようとしています。 簡単な解決策がない場合は、ヒントがあれば、より効果的な検索に役立ちます。 ID付きPHP:値をキーとして多次元配列から2次元配列を作成する

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

として:

私は、この複雑な配列(短縮たくさん)を持っているし、新しい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 (
       ... 

これは私が作成したいアレイでありますキーとその値:

$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を追加しました!これがあなたの問題を解決したので、投票して答えを受け入れてください。 –

+0

は受け入れて投票しましたが(まだ私の最初の質問、まだ学んでいます)、私の投票は表示されませんが、評判が15未満です – LeoRon