3

Given this array

$inventory = array(
    "sdfsdfsdsx65fsdf1"=>array("type"=>"fruit", "price"=>3.50),
    "sdfsdfsdsx65fsdf2"=>array("type"=>"milk", "price"=>2.90),
    "sdfsdfsdsx65fsdf3"=>array("type"=>"pork", "price"=>5.43)
);

I want output like below

$inventory = array(
    "sdfsdfsdsx65fsdf3"=>array("type"=>"pork", "price"=>5.43),
    "sdfsdfsdsx65fsdf1"=>array("type"=>"fruit", "price"=>3.50),
    "sdfsdfsdsx65fsdf2"=>array("type"=>"milk", "price"=>2.90)  
);
Darshak Shah
  • 159
  • 8

1 Answers1

1
$inventory = array(
  array("sdfsdfsdsx65fsdf1"=>array("type"=>"fruit", "price"=>3.50)),
  array("sdfsdfsdsx65fsdf2"=>array("type"=>"milk", "price"=>2.90)),
  array("sdfsdfsdsx65fsdf3"=>array("type"=>"pork", "price"=>5.43))
);

usort($inventory, function($a, $b) {
  foreach ($a as $a);
  foreach ($b as $b);
  if ($a['price'] == $b['price']) return 0;
  return ($a['price'] < $b['price']) ? 1 : -1;
});

print_r($inventory);
hlpiii
  • 161
  • 1
  • 3
  • Can you give me output of Array ( [sdfsdfsdsx65fsdf1] => Array ( [type] => fruit [price] => 3.5 ) [sdfsdfsdsx65fsdf2] => Array ( [type] => milk [price] => 2.9 ) [sdfsdfsdsx65fsdf3] => Array ( [type] => pork [price] => 5.43 ) ) – Darshak Shah Feb 08 '16 at 06:43
  • Yes, Your output is write but I have edit the question please check once and please give me a output – Darshak Shah Feb 08 '16 at 06:48