What are the best practices for handling and displaying nested arrays in PHP?

When handling nested arrays in PHP, it is important to properly iterate through the arrays to access and display the nested elements. One way to do this is by using nested foreach loops to iterate through each level of the array. This allows you to access and display the nested elements in a structured and organized manner.

<?php

$nestedArray = [
    'first_level' => [
        'second_level' => [
            'third_level' => 'value'
        ]
    ]
];

foreach ($nestedArray as $firstLevelKey => $firstLevelValue) {
    echo "First Level Key: $firstLevelKey\n";
    
    foreach ($firstLevelValue as $secondLevelKey => $secondLevelValue) {
        echo "\tSecond Level Key: $secondLevelKey\n";
        
        foreach ($secondLevelValue as $thirdLevelKey => $thirdLevelValue) {
            echo "\t\tThird Level Key: $thirdLevelKey, Value: $thirdLevelValue\n";
        }
    }
}
?>