1

I'm using foreach to echo array multidimensional array.

I know there have another simple way but I want to try using foreach.

here is my code:

<?php
$cars = array(
    array("Volvo", 22, 18),
    array("BMW", 15, 13, 
        array(
            'satu' => 'heelo'
        )
    ),
    array("Saab", 5, 2),
    array("Land Rover", 17, 15, 
        array('satu','dua')
    )
);

foreach ($cars as $kebal)
{
    if (is_array ($kebal))
    {
        foreach ($kebal as $katak)
        {
            echo($katak);
            echo "<br>";
            echo "<br>";            
        }
    }
    else
    {
        echo ($kebal);
        echo "<br>";    
    }
}
?>

and the output i get is :

Volvo

22

18

BMW

15

13


Notice: Array to string conversion in C:\xampp\htdocs\latihtubi\dataarray.php on line 42
Array

Saab

5

2

Land Rover

17

15


Notice: Array to string conversion in C:\xampp\htdocs\latihtubi\dataarray.php on line 42
Array

so, how to properly write the code? Thanks.

5
  • 2
    loop aren't deep enough, you still got another dimension array('satu'=>'heelo'), just roll up a recursive function or use SPL Commented Mar 23, 2016 at 2:34
  • @Rizier123 how? foreach again using is_array? Commented Mar 23, 2016 at 2:35
  • @Ghost spl? sorry i'm still learning.. Commented Mar 23, 2016 at 2:36
  • you can just search for it, stackoverflow.com/questions/19709410/…, thisinterestsme.com/… just research your way, there's a lot of content Commented Mar 23, 2016 at 2:38
  • @Ghost thanks for your suggestion. now reading and trying :) Commented Mar 23, 2016 at 2:43

2 Answers 2

2

...but I want to try using foreach.

Ok - the reason your code isn't quite working is that when you reach the array e.g. satu => heelo you're just trying to echo it, because you aren't handling it at that point.

Just add another foreach to the outside:

foreach ($cars as $carOptions) {
    foreach ($carOptions as $kebal) {
        if (is_array($kebal)) {
            foreach ($kebal as $katak) {
                echo $katak;
                echo "<br>";
                echo "<br>";            
            }
        } else {
            echo $kebal;
            echo "<br>";
        }
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

your suggestion and explanation really clear me up! now I can understand how to use foreach for multidimensional array if there have many array in array. thank!
@MuhammadMyy - while this answer works, and fits your question. Realize that if your array structure changes, you may run into another issue.
I totally agree with @ThrowBackDewd - you should use a recursive function like in his answer in case it does change. It's also tidier than nested loops
2

The problem you're having is most of the elements of your array are either strings or integers, but you also have 2 elements that are arrays. You're best bet is to test if the value is an array first, then decide what to do.

<?php
    $cars = array(
        array("Volvo",22,18),
        array("BMW",15,13,array('satu'=>'heelo')),
        array("Saab",5,2),
        array("Land Rover",17,15,array('satu','dua'))
    );

    function myLoop($inputData){
        if( is_array($inputData) ){
            foreach($inputData as $value){
                myLoop($value);
                echo "<br>";
            }
        } else {
            echo "$inputData<br>";
        }
    }

    myLoop($cars);
?>

2 Comments

@RobbieAverill, thanks for that. I had a mistake, forgot my "foreach" loop
thanks both of you. i will keep your suggestion for further use in array. thanks again!

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.