What are some alternative approaches to generating nested lists in PHP besides using array_walk_recursive()?
When generating nested lists in PHP, an alternative approach to using array_walk_recursive() is to use recursive functions. By creating a function that calls itself for each nested level of the array, you can easily generate the desired nested list structure without relying on array_walk_recursive().
function generateNestedList($array) {
echo '<ul>';
foreach ($array as $key => $value) {
echo '<li>' . $key;
if (is_array($value)) {
generateNestedList($value);
} else {
echo ': ' . $value;
}
echo '</li>';
}
echo '</ul>';
}
$array = [
'fruit' => [
'apple' => 'red',
'banana' => 'yellow'
],
'vegetable' => 'carrot'
];
generateNestedList($array);