There is such an array

$inventory['status'] = 'success'; $inventory['weapons'][$i]['id'] = $itemID; $inventory['weapons'][$i]['name'] = $name; $inventory['weapons'][$i]['title'] = $market_name; $inventory['weapons'][$i]['quality'] = $quality; $inventory['weapons'][$i]['price'] = ($price >= 10)? round($price - ($price * 0.1), 0) : round($price - 2, 0); 

I need to sort it by descending price .

I tried to use sort, usort, but as I understood it. They sort only arrays of the type [1,3,2];

Preferred sample code.

  • Sort in descending order of what element? - cheops
  • Price, forgot to write. - Slavik Okara

1 answer 1

You can use the uasort() function, passing it as the second parameter an anonymous function that sets the sorting logic of a complex array element (by price element)

 <?php $inventory['weapons'][0]['price'] = 10; $inventory['weapons'][1]['price'] = 20; uasort($inventory['weapons'], function($a, $b) { if ($a['price'] == $b['price']) return 0; return ($a['price'] > $b['price']) ? -1 : 1; }); echo "<pre>"; print_r($inventory);