What are potential drawbacks of using nested foreach loops in PHP?

Using nested foreach loops can lead to performance issues, especially when dealing with large datasets. Each iteration of the inner loop is executed for every iteration of the outer loop, resulting in a high number of iterations. To solve this issue, consider using alternative methods such as array functions or restructuring the data to reduce the number of nested loops.

// Example of restructuring data to avoid nested loops
$data = [
    ['name' => 'John', 'age' => 30],
    ['name' => 'Jane', 'age' => 25],
    ['name' => 'Bob', 'age' => 35]
];

// Restructure data into associative array
$restructuredData = [];
foreach ($data as $item) {
    $restructuredData[$item['name']] = $item['age'];
}

// Use single loop to iterate over restructured data
foreach ($restructuredData as $name => $age) {
    echo "$name is $age years old\n";
}