In what ways can PHP be optimized for handling a large number of prices that overlap in a pricing table?

When dealing with a large number of prices that overlap in a pricing table, one way to optimize PHP is to use a multidimensional array to store the prices. This allows for efficient retrieval and comparison of prices, making it easier to handle overlapping prices. Additionally, using functions to calculate and update prices can help streamline the code and improve performance.

// Sample multidimensional array to store prices
$prices = [
    ['product_id' => 1, 'price' => 10],
    ['product_id' => 2, 'price' => 15],
    ['product_id' => 3, 'price' => 20],
    // Add more prices as needed
];

// Function to retrieve price for a given product ID
function getPrice($product_id, $prices) {
    foreach ($prices as $price) {
        if ($price['product_id'] == $product_id) {
            return $price['price'];
        }
    }
    return null; // Return null if price not found
}

// Example of how to use the getPrice function
$product_id = 2;
$price = getPrice($product_id, $prices);
echo "Price for product ID $product_id is $price";