What are the benefits of using associative arrays in PHP to store product IDs and quantities instead of traditional indexed arrays?

When storing product IDs and quantities, using associative arrays in PHP allows for easy retrieval of quantities based on product IDs without needing to loop through the array. This provides a more efficient way to manage and update quantities for specific products. Additionally, associative arrays allow for clearer and more readable code compared to traditional indexed arrays when dealing with key-value pairs.

// Using associative arrays to store product IDs and quantities
$productQuantities = [
    'product_id_1' => 5,
    'product_id_2' => 10,
    'product_id_3' => 3
];

// Retrieve quantity for a specific product ID
$productId = 'product_id_2';
$quantity = $productQuantities[$productId];
echo "Quantity for product ID $productId: $quantity";