您的位置:首页 > 编程语言 > PHP开发

PHP二维数组排序(list_order)

2016-07-14 01:16 711 查看
/**
* 对二维数组进行排序
* 模拟 数据表记录按字段排序
*
* <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);
}
}

应用:

@list_order($list, $get['orderKey'], $get['orderType'], "string");


延伸阅读:

PHP array_multisort() 函数详解 及 二维数组排序(模拟数据表记录按字段排序)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: