1

I have a multidimensional array with key value. I want to loop the data in that array but I don't know how.

This is my 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
                    )

            )

    );

How do I loop / foreach that array?

I guess there is a foreach inside a foreach, but I don't know how to do that.

Henders
  • 1,195
  • 1
  • 21
  • 27
Dimas Adi Andrea
  • 443
  • 3
  • 11
  • 25
  • 3
    Possible duplicate of [how can i get multidimensional array values using foreach?](http://stackoverflow.com/questions/16356768/how-can-i-get-multidimensional-array-values-using-foreach) – MonkeyZeus Aug 02 '16 at 12:54
  • `foreach($myArray as $outerkey => $outervalue) { foreach($outervalue as $innerkey => $innervalue) { [[do something]] } }` where outerkey would be 134 and innerkey would be 1138, and innervalue would be the array with id and qty and so on. – Jakumi Aug 02 '16 at 12:55
  • @Dimas If one of the answers has solved your question, please award the green tick to the most helpful correct answer. – mickmackusa Feb 03 '18 at 08:50
  • @Jakumi please never post solutions as comments on StackOverflow. Please delete your comment. If you want to post a solution, please post an answer. – mickmackusa Feb 03 '18 at 08:52
  • @mickmackusa it's been half a year, the question should have been deleted, since it's a duplicate. there is an answer that solves the question. and although you might be technically right, I believe noone gaf. – Jakumi Feb 04 '18 at 18:55
  • What ever you decide @Jakumi , answer or don't answer. Either way please delete your commented solution. – mickmackusa Feb 04 '18 at 21:31

3 Answers3

3

You can use a recursive function here, so you don't need to worry how deep your array will be.

Something like this:

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);
Davide Perozzi
  • 386
  • 2
  • 13
2

Just use another foreach inside your foreach

foreach ($myArray as $key => $value) {
    foreach ($value as $subkey => $subvalue) {
        if (is_array($subvalue)) {
            foreach ($subvalue as $subsubkey => $subsubvalue) {
                // .... and so on
            }
        }
    }
}
Peter
  • 8,776
  • 6
  • 62
  • 95
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;
            }
        }
    }
}
Seth Battis
  • 106
  • 8