How to sort an array of associative arrays by value of a given key in PHP?

前端 未结 19 1849
清酒与你
清酒与你 2020-11-21 23:34

Given this array:

$inventory = array(

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


        
相关标签:
19条回答
  • 2020-11-22 00:39

    While others have correctly suggested the use of array_multisort(), for some reason no answer seems to acknowledge the existence of array_column(), which can greatly simplify the solution. So my suggestion would be:

    array_multisort(array_column($inventory, 'price'), SORT_DESC, $inventory);
    
    0 讨论(0)
提交回复
热议问题