How can the redundancy in the provided PHP code be eliminated to improve efficiency and readability?
The redundancy in the provided PHP code can be eliminated by using a loop to iterate over the array of product names and quantities, instead of repeating the same code block multiple times for each product. By using a loop, we can improve efficiency, readability, and maintainability of the code.
<?php
$products = [
['name' => 'Product A', 'quantity' => 5],
['name' => 'Product B', 'quantity' => 3],
['name' => 'Product C', 'quantity' => 2]
];
foreach ($products as $product) {
echo "Product: " . $product['name'] . ", Quantity: " . $product['quantity'] . "<br>";
}
?>