Display an array in a readable/hierarchical format
Here is an example of how you could display an array in a readable/hierarchical format in PHP:
<?php
function print_array($arr, $indent = 0)
{
foreach ($arr as $item) {
if (is_array($item)) {
print_array($item, $indent + 1);
} else {
echo str_repeat(" ", $indent) . $item . "\n";
}
}
}
$my_array = [1, 2, [3, 4, [5, 6], 7], 8];
print_array($my_array);
This will output the following:
1 2 3 4 5 6 7 8
Watch a video course
Learn object oriented PHP
This function works by using recursion to iterate through the array and adding indentation for each level of hierarchy. If an item in the array is itself an array, then the function calls itself to process that subarray. If the item is not an array, it is simply printed with the appropriate indentation.
I hope this helps! Let me know if you have any questions.