PHP二维数组排序(list_order)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | /** * 对二维数组进行排序 * 模拟 数据表记录按字段排序 * * <code> * @list_order($list, $get['orderKey'], $get['orderType']); * </code> * @param array $array 要排序的数组 * @param string $orderKey 排序关键字/字段 * @param string $orderType 排序方式,'asc':升序,'desc':降序 * @param string $orderValueType 排序字段值类型,'number':数字,'string':字符串 * @link http://www.cnblogs.com/52php/p/5668809.html */ function list_order(& $array , $orderKey , $orderType = 'asc' , $orderValueType = 'string' ) { if ( is_array ( $array )) { $orderArr = array (); foreach ( $array as $val ) { $orderArr [] = $val [ $orderKey ]; } $orderType = ( $orderType == 'asc' ) ? SORT_ASC : SORT_DESC; $orderValueType = ( $orderValueType == 'string' ) ? SORT_STRING : SORT_NUMERIC; array_multisort ( $orderArr , $orderType , $orderValueType , $array ); } } |
应用:
1 | @list_order( $list , $get [ 'orderKey' ], $get [ 'orderType' ], "string" ); |
延伸阅读:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步