标签:des style blog http io ar color 使用 sp
一维数组排序可以使用asort、ksort等一些方法进程排序,相对来说比较简单。二维数组的排序怎么实现呢?使用array_multisort和usort可以实现
$users = array( array(‘name‘ => ‘tom‘, ‘age‘ => 20) , array(‘name‘ => ‘anny‘, ‘age‘ => 18) , array(‘name‘ => ‘jack‘, ‘age‘ => 22) );
$ages = array(); foreach ($users as $user) { $ages[] = $user[‘age‘]; } array_multisort($ages, SORT_ASC, $users);
array_multisort($ages, SORT_ASC, $names, SORT_ASC, $users);
usort($users, function($a, $b) { $al = strlen($a[‘name‘]); $bl = strlen($b[‘name‘]); if ($al == $bl) return 0; return ($al > $bl) ? -1 : 1; });
$ages = array(); $scores = array(); $users = array( array(‘name‘ => ‘tom‘, ‘age‘ => 20,‘size‘=>22,‘score‘=>100) ,array(‘name‘ => ‘gang‘,‘age‘ => 20,‘size‘=>20, ‘score‘=>80) , array(‘name‘ => ‘anny‘,‘age‘ => 20,‘size‘=>20, ‘score‘=>90) , array(‘name‘ => ‘jack‘,‘age‘ => 22,‘size‘=>20, ‘score‘=>50) ); foreach ($users as $user) { $ages[] = $user[‘age‘]; $scores[] = $user[‘score‘]; } array_multisort($ages,SORT_DESC,$scores,SORT_DESC, $users); echo "<pre>"; print_r($user)
这样实现数据的order by ? and ?的排序功能,按照猜测该排序不会被另外一个具有相同元素但是元素不同排序打乱(比如上面的age和size元素作为数组具有同样的元素,但是元素次序不同),
简单来讲排序绝对匹配参数数组。
php二维数组排序方法(转自http://www.3lian.com/edu/2013/12-26/118882.html)
标签:des style blog http io ar color 使用 sp
原文地址:http://www.cnblogs.com/yuerdongni/p/4142226.html