0

Hello I am having hard time looping through the PHP multidimensional array, I want to know the best possible way of looping an array. This is the current array that I am trying to loop through.

Array
(
    [bathroom] => Array
        (
            [name] => Bathroom
            [things] => Array
                (
                    [0] => Array
                        (
                            [name] => Cheval Mirrow
                            [cubic] => .14
                            [quantity] => 1
                        )

                    [1] => Array
                        (
                            [name] => Carton/Wine
                            [cubic] => .07
                            [quantity] => 1
                        )

                    [2] => Array
                        (
                            [name] => Carton/picture
                            [cubic] => .07
                            [quantity] => 1
                        )


                )

        )

)

I have tried this code

$keys = array_keys($array);
for($i = 0; $i < count($array); $i++) {
    echo $keys[$i] . "<br>";
    foreach($array[$keys[$i]] as $key => $value) {
        echo $key . " : " . $value . "<br>";

        foreach($array[$value[$i]] as $key1 => $value1){

            echo $key1.":". $value1."<br>";
        }


    }
    echo "<br>";
}

I am able to get the first value now the issues is that I am not able to get the values of things array, I am getting error on this, can someone tell me where I am getting wrong on this.

Abbasi
  • 588
  • 1
  • 7
  • 26
  • Possible duplicate of [PHP foreach loop through multidimensional array](https://stackoverflow.com/questions/842956/php-foreach-loop-through-multidimensional-array) – Russ J Feb 23 '19 at 02:33
  • Possible duplicate of [Loop through an array php](https://stackoverflow.com/questions/4414623/loop-through-an-array-php) – Nick Feb 23 '19 at 02:35
  • Which part of the array do you want to loop through, or rather what data do you want to retrieve? – Progrock Feb 23 '19 at 05:45

2 Answers2

2

Here's an example of how you can process your array:

foreach ($array as $key => $value) {
    echo "$key:<br>\n";
    echo "    name: {$value['name']}<br>\n";
    foreach ($value['things'] as $t => $thing) {
        echo "\tthing $t:<br>\n";
        foreach ($thing as $name => $val) {
            echo "\t    $name: $val<br>\n";
        }
    }
}

Output:

bathroom:<br>
  name: Bathroom<br>
  thing 0:<br>
    name: ChevalMirrow<br>
    cubic: 0.14<br>
    quantity: 1<br> 
  thing 1:<br> 
    name: Carton/Wine<br>
    cubic: 0.07<br>
    quantity: 1<br>
  thing 2:<br>
    name: Carton/picture<br>
    cubic: 0.07<br>
    quantity: 1<br>

Demo on 3v4l.org

Nick
  • 138,499
  • 22
  • 57
  • 95
  • You forgot to include/update the "thing number variable", otherwise great. – Secko Feb 23 '19 at 04:19
  • @Secko thanks for that. I updated the code in the demo (and the link) but forgot to copy the new code back into the answer. I've updated my answer with the new code now. – Nick Feb 23 '19 at 04:20
0
foreach ($orginalarray as $key1 => $value1){
    foreach ($value1 as $key2 => $value2) {
        foreach ($value2 as $key3 => $value3) {  
            foreach ($value3 as $key3 => $value3) { 

            } 
        }
    }
}
RTO
  • 1
  • 2
  • @Iters How can I echo value within the loop? – Abbasi Feb 23 '19 at 02:52
  • Either echo $value2, or echo print_r($value2). I typically collect info in a var and then push it to the screen all at once. $info .= "new information." // keep adding data and the at the end echo $info;$info=""; – RTO Feb 23 '19 at 02:54
  • I did that and on the third tear I am getting this error Warning: Invalid argument supplied for foreach() in /home/fragile/public_html/test/index.php on line 63 things:Array0: Array1: Array2: Array3: Array4: Array5: Array6: Array7: Array – Abbasi Feb 23 '19 at 03:02
  • Do you know how deep it goes? You can use echo print_r($toparryvar); to see the full variable you are dealing with. Each new foreach, should take the previous value and make a new key and and new value var, don't reuse the upper level ones. If you do, you will break the upper loops. I would use the SIMPLE foreach($value... – RTO Feb 23 '19 at 03:03
  • I put the array on my question on top this is deep the array will go – Abbasi Feb 23 '19 at 03:08
  • This is what the print_r shows up (https://prnt.sc/moxliq) I have to loop that last array, When I try to use foeach I am getting invalid argument supplied to foreach error. – Abbasi Feb 23 '19 at 03:26