PHP – Using Recursion to print out values in a multidimensional array.

This is a PHP tutorial on how to print out all of the keys and values in a multidimensional array.

For this particular example, we are going to presume that we do not know how many levels the array in question will have.

In other words, we will write a PHP script that will echo out all keys and values in a given array, regardless of how many levels it has.

In these examples, we will be using a process called recursion.

Printing out a multidimensional array using PHP.

Our first example will be pretty simple, just in case you are new to the world of PHP.

Firstly, we will create a basic array.

$myArray = array(
    'example',
    'example two',
    array(
        'another level',
        array(
            'level three'
        )
    )
);

As you can see, we have created a multidimensional array with three levels.

To print the values out, we will create a recursive function.

function recursive($array){
    foreach($array as $key => $value){
        //If $value is an array.
        if(is_array($value)){
            //We need to loop through it.
            recursive($value);
        } else{
            //It is not an array, so print it out.
            echo $value, '<br>';
        }
    }
}

When we pass our array into the function above, it will loop through each value in the array.

Inside the loop, we have an IF statement that checks to see if the current value is an array.

IF the value is NOT an array, then it will print it out to the page.

However, IF the value in question is an array, then we will pass it to our function so that the same process can repeat itself.

Try it out for yourself! Provide the recursive function above with your own custom array, just so you can see it in action.

When it comes to programming, it is always better to get your hands dirty.

Indenting.

What if we want to indent the values as we are printing them out? Or what if we want to see which level each value is on?

In this case, we will add another parameter called $level, which will be incremented whenever we “take a step down”.

function recursive($array, $level = 1){
    foreach($array as $key => $value){
        //If $value is an array.
        if(is_array($value)){
            //We need to loop through it.
            recursive($value, $level + 1);
        } else{
            //It is not an array, so print it out.
            echo str_repeat("-", $level), $value, '<br>';
        }
    }
}

As you can see, the $level variable has a default value of 1.

Whenever we make a recursive call to our function, we will increment our $level variable by 1.

We then use the str_repeat function, which will repeat the hyphen symbol a certain number of times.

In this case, we are using our $level variable as the multiplier. In other words, if $level is equal to three, then our script will output three hyphen symbols.

A quick example of the str_repeat function in action.

<?php
echo str_repeat("*", 7);

//Result:
//*******

If you run the code above, you will see that it prints seven asterisks characters.

Printing out keys.

Printing out the keys of your array is trivial once you have figured out how to use recursion to print out its values.

function recursive($array, $level = 1){
    foreach($array as $key => $value){
        //If $value is an array.
        if(is_array($value)){
            //We need to loop through it.
            recursive($value, $level + 1);
        } else{
            //It is not an array, so print it out.
            echo $key . ": " . $value, '<br>';
        }
    }
}

In this, we only needed to make one small change to the original function!