How can PHP loops be optimized to handle multiple data entries in nested arrays without duplicating outputs?

When dealing with nested arrays in PHP loops, it is important to avoid duplicating outputs by properly structuring the loop logic. One way to optimize this is by using a combination of foreach loops and conditional statements to iterate through the data entries efficiently and avoid duplicate outputs.

$data = [
    'users' => [
        ['name' => 'John', 'age' => 30],
        ['name' => 'Jane', 'age' => 25],
    ],
    'products' => [
        ['name' => 'Laptop', 'price' => 1000],
        ['name' => 'Phone', 'price' => 500],
    ]
];

foreach ($data as $key => $items) {
    echo ucfirst($key) . ":\n";
    foreach ($items as $item) {
        echo "- " . $item['name'] . " - $" . $item['price'] . "\n";
    }
    echo "\n";
}