What are the potential issues with using nested foreach loops in PHP for data retrieval and display?

Potential issues with using nested foreach loops in PHP for data retrieval and display include decreased performance due to the increased complexity of the loops, potential for errors if the data structure is not consistent, and difficulty in maintaining and debugging the code. To solve this issue, consider restructuring the data retrieval process to reduce the need for nested loops, or use alternative methods such as array functions to manipulate the data more efficiently.

// Example of restructuring data retrieval to reduce nested loops
$data = [
    [
        'id' => 1,
        'name' => 'John',
        'orders' => [
            ['id' => 101, 'product' => 'Item A'],
            ['id' => 102, 'product' => 'Item B']
        ]
    ],
    [
        'id' => 2,
        'name' => 'Jane',
        'orders' => [
            ['id' => 201, 'product' => 'Item C']
        ]
    ]
];

foreach ($data as $customer) {
    echo "Customer: {$customer['name']}\n";
    
    foreach ($customer['orders'] as $order) {
        echo "Order ID: {$order['id']}, Product: {$order['product']}\n";
    }
}