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.

3
  • Possible duplicate of PHP foreach loop through multidimensional array Commented Feb 23, 2019 at 2:33
  • Possible duplicate of Loop through an array php Commented Feb 23, 2019 at 2:35
  • Which part of the array do you want to loop through, or rather what data do you want to retrieve? Commented Feb 23, 2019 at 5:45

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

You forgot to include/update the "thing number variable", otherwise great.
@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.
0
foreach ($orginalarray as $key1 => $value1){
    foreach ($value1 as $key2 => $value2) {
        foreach ($value2 as $key3 => $value3) {  
            foreach ($value3 as $key3 => $value3) { 

            } 
        }
    }
}

6 Comments

@Iters How can I echo value within the loop?
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="";
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
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...
I put the array on my question on top this is deep the array will go
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.