php二维数组排序
PHP中可以使用array_multisort()
函数对二维数组进行排序。
示例代码:
// 定义二维数组 $fruits = array( array("name" => "apple", "color" => "red", "weight" => 120), array("name" => "banana", "color" => "yellow", "weight" => 80), array("name" => "orange", "color" => "orange", "weight" => 100) ); // 按照weight字段进行升序排序 $weights = array_column($fruits, "weight"); array_multisort($weights, SORT_ASC, $fruits); // 输出排序后的数组 print_r($fruits);
输出结果:
Array ( [0] => Array ( [name] => banana [color] => yellow [weight] => 80 ) [1] => Array ( [name] => orange [color] => orange [weight] => 100 ) [2] => Array ( [name] => apple [color] => red [weight] => 120 ) )
可以看到,按照weight字段进行升序排序后,输出结果是一个按照weight逐渐递增的数组。
0 条评论
登陆后才能评论哦~