• 234

php对二维数组排序

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

请教大家php怎么写

阅读 267
评论
    3 个回答
    • 40
    $arr = array(
      'apple' => array('weight'=>'3'),
      'orange' => array('weight'=>'5'),
      'banana' => array('weight'=>'2'),
      'tomato' => array('weight'=>'1'),
    );
    $weight = array_column($arr, 'weight');
    array_multisort($weight, SORT_ASC, $arr);
    var_dump($arr);