PHP实现-二维数组排序

2018-03-21 10:15 By "Powerless" 3610 0 1

/**
 * 对二维数组进行排序
 * 模拟 数据表记录按字段排序
 *
 * @param array $array 要排序的数组
 * @param string $orderKey 排序关键字/字段
 * @param string $orderType 排序方式,'asc':升序,'desc':降序
 * @param string $orderValueType 排序字段值类型,'number':数字,'string':字符串
 */
public function listOrder($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);
        return $array;
    }
}

另外:array_multisort 函数功能也很强大,详细可以参看PHP手册

评 论

View in WeChat

Others Discussion

  • 2018年云计算热词
    Posted on 2019-06-12 18:19
  • PHP 基金会来啦!
    Posted on 2022-10-08 17:40
  • 快速了解Kafka
    Posted on 2021-03-25 14:20
  • Redis各种数据类型的使用场景举例分析【二】
    Posted on 2018-11-22 10:30
  • TCP协议的特性
    Posted on 2019-04-26 16:46
  • 让你的PHP7更快(GCC PGO)
    Posted on 2018-03-07 14:09
  • Composer 异常 [ErrorException]
    Posted on 2019-11-25 17:55
  • BASE原则
    Posted on 2020-12-17 16:42