• 234

php对二维数组排序

排序前
$arr = array(
  'apple' => array('weight'=>'3'),
  'orange' => array('weight'=>'5'),
  'banana' => array('weight'=>'2'),
  'tomato' => array('weight'=>'1'),
);
排序后(根据weight排序)
?

请教大家php怎么写

阅读 275
评论
    3 个回答
    $arr = [
        ['name' => 'apple', 'weight' => '3'],
        ['name' => 'orange', 'weight' => '5'],
        ['name' => 'banana', 'weight' => '2'],
        ['name' => 'tomato', 'weight' => '1'],
    ];
    
    usort($arr, function ($current, $next): int {
        return $current['weight'] > $next['weight'] ? 1 : -1;
    });
    
    var_dump($arr);