What are the potential pitfalls of using multiple while loops and foreach loops in PHP?

Using multiple while loops and foreach loops in PHP can lead to nested loop structures that are hard to read, debug, and maintain. It can also result in inefficient code execution, especially if the loops are iterating over large datasets. To solve this issue, consider refactoring the code to use functions or classes to encapsulate the logic within each loop, breaking down the complex logic into smaller, more manageable parts.

// Example of refactoring nested loops into functions

function processNestedData($data) {
    foreach($data as $item) {
        // Process each item
    }
}

function processMultipleData($data) {
    foreach($data as $item) {
        // Process each item
    }
}

// Usage
$data1 = [/* data */];
$data2 = [/* data */];

processNestedData($data1);
processMultipleData($data2);