对于listing j示例为我们具体说明了php函数array_multisort()的工作方式:
php $data = array(array(id => 1, name => boney m, rating => 3), array(id => 2, name => take that, rating => 1), array(id => 3, name => the killers, rating => 4), array(id => 4, name => lusain, rating => 3), ); foreach ($data as $key => $value) { $name[$key] = $value['name']; $rating[$key] = $value['rating']; } array_multisort($rating, $name, $data); print_r($data);?>
这里,我们在$data数组中模拟了一个行和列数组。然后,我使用php函数array_multisort()对数据集合进行重排,首先是根据rating进行排序,然后,如果rating相等的话,再根据name排序。它的输出结果如下:
array ([0] => array
(
[id] => 2
[name] => take that
[rating] => 1
) [1] => array
(
[id] => 1
[name] => boney m
[rating] => 3
)
[2] => array
(
[id] => 4
[name] => lusain
[rating] => 3
)
[3] => array
(
[id] => 3
[name] => the killers
[rating] => 4
)
)
php函数array_multisort()是php中最有用的函数之一,它有非常广泛的应用范围。另外,就如你在例子中所看到的,它能对多个不相关的数组进行排序,也可以使用其中的一个元素作为下次排序的基础,还可以对数据库结果集进行排序。
http://www.bkjia.com/phpjc/446344.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/446344.htmltecharticle对于 listing j示例为我们具体说明了php函数array_multisort()的工作方式: ? php $ data = array (array(id= 1,name= boneym,rating= 3), array(id= 2,name=...