【PHP】php 二维关联数组的对某一个值排序
程序员文章站
2022-05-08 20:10:25
...
$arr=Array ( [0] =Array ( [id] =169, [sorts] =4, [fans] = 0, [created_at] =1413621597 ) [1] =Array ( [id] =170, [sorts] =5, [fans] = 5, [created_at] =1418621597 ) [2] =Array ( [id] =171, [sorts] =1, [fans] = 9, [created_at] =1413621597 ) )
$arr=Array
(
[0] =Array
(
[id] =169,
[sorts] =4,
[fans] = 0,
[created_at] =1413621597
)
[1] =Array
(
[id] =170,
[sorts] =5,
[fans] = 5,
[created_at] =1418621597
)
[2] =Array
(
[id] =171,
[sorts] =1,
[fans] = 9,
[created_at] =1413621597
)
)
$arr是一个二维的关联数组,要对sorts的值按顺序/倒叙排序,需要用到array_multisort( )这个函数。
这个函数第一个参数是数组(必填,后面参数可选),第二个是排序方式SORT_ASC默认升序,SORT_DESC 降序,第三个SORT_REGULAR默认将每一项按常规顺序排列,SORT_NUMERIC按数字顺序排列,SORT_STRING按字母顺序排列,第四个是外层数组$arr
foreach ($arr as $key => $value) {
$temp[$key]=$value['sorts'];
}
array_multisort($temp,SORT_ASC,$arr);
print_r($arr);即可
上一篇: MSSQL 事务说明
下一篇: linux下讲解MySQL安装与登录方法