Sort array of arrays by sub-array values
We need to sort the array by
price
value
$inventory = array(

   array("type"=>"fruit", "price"=>3.50),
   array("type"=>"milk", "price"=>2.90),
   array("type"=>"pork", "price"=>5.43),

);
The solution is to use
array_multisort
function
$price = array();
foreach ($inventory as $key => $row)
{
    $price[$key] = $row['price'];
}
array_multisort($price, SORT_DESC, $inventory);
In PHP 5.5 or later
array_column
function can be used
$price = array_column($inventory, 'price');

array_multisort($price, SORT_DESC, $inventory);
by Valeri Tandilashvili
3 years ago
PHP
0
Pro tip: use ```triple backticks around text``` to write in code fences