In what scenarios would it be more efficient to combine multiple loops into a single loop in PHP?

Combining multiple loops into a single loop in PHP can be more efficient when you are iterating over the same data multiple times in separate loops. By consolidating the logic into a single loop, you can reduce the overhead of iterating over the data multiple times and improve the overall performance of your code.

// Example of combining multiple loops into a single loop
$data = [1, 2, 3, 4, 5];

// Separate loops
foreach ($data as $item) {
    // Do something with $item
}

foreach ($data as $item) {
    // Do something else with $item
}

// Combined loop
foreach ($data as $item) {
    // Do something with $item
    // Do something else with $item
}