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

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

Given this array:

$inventory = array(

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


        
19条回答
  •  梦毁少年i
    2020-11-22 00:18

    I use uasort like this

     'joe',
            'age' => 11
        ],
        [
            'username' => 'rakoto',
            'age' => 21
        ],
        [
            'username' => 'rabe',
            'age' => 17
        ],
        [
            'username' => 'fy',
            'age' => 19
        ],    
    ];
    
    
    uasort($users, function ($item, $compare) {
        return $item['username'] >= $compare['username']; 
    });
    
    var_dump($users);
    

提交回复
热议问题