2016-08-02 12 views
1

Ich habe ein multidimensionales Array mit Schlüsselwert. Ich möchte die Daten in diesem Array loopen, weiß aber nicht wie.Wie man multidimensionales Array mit Schlüsselwert foreach

Das ist mein Array:

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

      ) 

    ); 

Wie kann ich Schleife/das Array foreach?

Ich denke, es gibt eine Foreach in einer Foreach, aber ich weiß nicht, wie das geht.

+3

Mögliche Duplikat [? Wie kann ich mehrdimensionales Array Werte mit foreach get] (http://stackoverflow.com/questions/16356768/how-can-i -get-multidimensional-array-values-using-foreach) – MonkeyZeus

+0

'foreach ($ myArray als $ uterkey => $ outervalue) {foreach ($ outervalue als $ innerkey => $ innervalue) {[[etwas tun]]}}}' Der äußere Schlüssel wäre 134 und der innere Schlüssel wäre 1138 und der innere Wert wäre das Array mit ID und Menge und so weiter. – Jakumi

+0

@Dimas Wenn eine der Antworten Ihre Frage gelöst hat, vergeben Sie bitte den grünen Haken an die hilfreichste richtige Antwort. – mickmackusa

Antwort

2

Verwenden Sie einfach eine andere foreach in Ihrem foreach

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

Sie eine rekursive Funktion hier verwenden können, so dass Sie sich keine Sorgen machen, wie tief Ihr Array sein.

Etwas wie folgt aus:

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

Eine noch bessere Lösung. – 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

schön, das ist was ich brauche. ich danke dir sehr –