2

Let's say you have a multidimensional array like so and assume it won:

$Call = array(
    "Foo1" => array(
        "Bar1" => array(
            "Baz1", 
            "Baz2"
        )
    ), 
    "Foo2", 
    "Foo3" => array(
        "Bar2", 
        "Bar3"
    )
);

How would you echo Every Foo, Bar and Baz in the array? I'm particularly having an issue due to Foo2 being set key of 0, so I need to somehow workaround the key being set on values that are not keys.

My current solution, although works, feels too cheat and was wondering if there's cleaner way to loop through the array:

    foreach ($Call as $key => $value) {

            echo "1.".(is_array($value) ? $key : $value)."<br>";

        if(is_array($value)){

            foreach ($value as $key => $value) {

                echo "a.".(is_array($value) ? $key : $value)."<br>";

                if(is_array($value)){

                    foreach ($value as $key => $value) {

                        echo "A.".(is_array($value) ? $key : $value)."<br>";

                    }

                }

            }

        }

    }
1
  • What is the expected output for the input you posted? Commented Apr 3, 2018 at 10:02

2 Answers 2

1

For a clean code using recursion I have following solution.

$Call= array("Foo1" => array("Bar1" => array("Baz1", "Baz2")), "Foo2", "Foo3" => array("Bar2", "Bar3"));
cleanloop($Call);
function cleanloop($arr){
    foreach($arr as $key => $value){
        if(is_array($value)){
            echo $key . "<br/>";
            cleanloop($value);
        }else{
            echo $value . "<br/>";
        }
    }
}

I hope with a little more effort, listing / numbering can be managed. using html buitl in numbering I can update like

function cleanloop($arr){
    echo "<ol>";
    foreach($arr as $key => $value){
        if(is_array($value)){
            echo "<li>";
            echo $key;
            cleanloop($value);
            echo "</li>";
        }else{
            echo "<li>" . $value . "</li>";
        }
    }
    echo "</ol>";
}
Sign up to request clarification or add additional context in comments.

Comments

1

It is a matter of taste, but I do not like recursion.

The SPL iterators provide all you need:

<?php
$Call = array(
    "Foo1" => array(
        "Bar1" => array(
            "Baz1",
            "Baz2"
        )
    ),
    "Foo2",
    "Foo3" => array(
        "Bar2",
        "Bar3"
    )
);
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($Call), RecursiveIteratorIterator::SELF_FIRST);
for ($iterator->rewind(); $iterator->valid(); $iterator->next()) {
    echo 'Depth: ';
    echo $iterator->getDepth();
    echo ' Key: ';
    echo $iterator->key();
    echo ' Content: ';
    echo is_array($iterator->current()) ? 'Array' : $iterator->current();
    echo '; ' . PHP_EOL;
}

Please have a look at the constructor of the RecursiveIteratorIterator. It provides some useful flags.

Comments

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.