What is the best way to sort an array in PHP based on a specific key, such as price?

When sorting an array in PHP based on a specific key, such as price, you can use the `array_multisort()` function. This function allows you to sort multiple arrays or a multi-dimensional array by one or more key values. You can specify the key you want to sort by and the sorting order (ascending or descending).

// Sample array to sort by price
$products = [
    ['name' => 'Product A', 'price' => 50],
    ['name' => 'Product B', 'price' => 30],
    ['name' => 'Product C', 'price' => 40]
];

// Extract the prices from the products array
foreach ($products as $key => $product) {
    $prices[$key] = $product['price'];
}

// Sort the products array based on price
array_multisort($prices, SORT_ASC, $products);

// Output the sorted array
print_r($products);