What are common challenges faced when generating HTML lists from PHP arrays?

One common challenge faced when generating HTML lists from PHP arrays is properly handling nested arrays to create nested lists. To solve this, you can recursively iterate through the array elements and generate the corresponding list items.

function generateListItems($array) {
    $output = '<ul>';
    foreach ($array as $item) {
        if (is_array($item)) {
            $output .= '<li>' . generateListItems($item) . '</li>';
        } else {
            $output .= '<li>' . $item . '</li>';
        }
    }
    $output .= '</ul>';
    return $output;
}

$array = array(
    'Item 1',
    'Item 2',
    array(
        'Nested Item 1',
        'Nested Item 2'
    ),
    'Item 3'
);

echo generateListItems($array);