2016-08-02 20 views
1

我有一個帶有鍵值的多維數組。我想循環數組中的數據,但我不知道如何。如何使用鍵值foreach多維數組

這是我的數組:

$myArray=  Array 
    (
     '134' => Array 
      (
       '1138' => Array 
        (
         'id' => 1138, 
         'qty' => 1, 
         'price' => 4900000, 
         'name' => 'Pioneer AVH X5850BT Head Unit Double Din 7Inch', 
         'options' => Array 
          (
           'image' => '865e6ad631fa45d408acfc6f8a8ff008.jpg', 
           'created_by' => 134 
          ), 

         'rowid' => 'f9e62f7ce9665a25ff40848744dd83f4', 
         'subtotal' => 4900000 
        ), 

       '1003' => Array 
        (
         'id' => 1003, 
         'qty' => 1, 
         'price' => 2250000, 
         'name' => 'Steelmate SW813 Subwoofer Aktif 10 Inch', 
         'options' => Array 
          (
           'image' => '962df806d5adc7bd0d22666fe996f139.jpg', 
           'created_by' => 134 
          ), 

         'rowid' => '7aa455ef597b2906e3895783bd7a5c70', 
         'subtotal' => 2250000 
        ) 

      ), 

     '157' => Array 
      (
       '2527' => Array 
        (
         'id' => 2527, 
         'qty' => 1, 
         'price' => 2475000, 
         'name' => 'Rockford Fosgate P165SE Speaker Split 2way Komponen', 
         'options' => Array 
          (
           'image' => '81027273a2ad59a96aec85ec66ce2704.jpg', 
           'created_by' => 157 
          ), 

         'rowid' => 'ed9301accd0d84bd0417609aa80cebc7', 
         'subtotal' => 2475000 
        ) 

      ) 

    ); 

如何循環/的foreach數組?

我想foreach裏有一個foreach,但我不知道該怎麼做。

+3

的[?我怎樣才能使用的foreach多維數組值(可能的複製http://stackoverflow.com/questions/16356768/how-can-i使用foreach) – MonkeyZeus

+0

'foreach($ myArray as $ outerkey => $ outervalue){foreach($ outervalue as $ innerkey => $ innervalue){[[do something]]}}'其中outerkey是134,innerkey是1138,innervalue是帶有id和qty等的數組。 – Jakumi

+0

@Dimas如果其中一個答案解決了您的問題,請將綠色標記獎勵給最有幫助的正確答案。 – mickmackusa

回答

2

只需使用另一個foreach裏面你foreach

foreach ($myArray as $key => $value) { 
    foreach ($value as $subkey => $subvalue) { 
     if (is_array($subvalue)) { 
      foreach ($subvalue as $subsubkey => $subsubvalue) { 
       // .... and so on 
      } 
     } 
    } 
} 
2

你可以在這裏使用一個遞歸函數,所以你不必擔心你的陣列將有多深。

事情是這樣的:

function read($arr) { 
    foreach ($arr as $key => $value) { 
     if (is_array($value)) { 
      read($a); 
     } 
     else { 
      // You cann access key and value here (for each array) 
     } 
    } 
} 

read($myArray); 
+0

甚至更​​好的解決方案。 – Peter

2
/* loop across $myArray (listing elements 134 and 157) */ 
foreach ($myArray as $groupId => $group) { 

    echo "walking through group $groupId" . PHP_EOL; 

    /* loop across "groups" 134 and 157 (listing elements 1138, 1003, etc.) */ 
    foreach ($group as $itemId => $item) { 

     echo "walking through item $itemId" . PHP_EOL; 

     /* loop each "item" record in key/value pairs */ 
     foreach ($item as $key => $value) { 

      echo "$key: "; 

      /* deal with options sub-array */ 
      if (is_array($value)) { 
       foreach ($value as $option => $optionValue) { 
        echo " $option: $optionValue" . PHP_EOL; 
       } 
      } else { 
       echo $value . PHP_EOL; 
      } 
     } 
    } 
} 
+0

不錯,這就是我需要的。非常感謝 –