PHP实现-二维数组排序

2018-03-21 10:15 By "Powerless" 3617 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手册

评 论

Others Discussion

  • 一些常见的基础概念
    Posted on 2018-11-28 19:10
  • HTTP头中隐藏PHP版本号
    Posted on 2021-01-11 16:38
  • MySQL中的行级锁,表级锁,页级锁
    Posted on 2018-08-25 11:00
  • QPS、TPS、RT、吞吐量到底是什么
    Posted on 2020-02-02 01:15
  • MySQL事务介绍
    Posted on 2019-06-05 18:14
  • 能创建多少个 TCP 连接?
    Posted on 2021-08-02 16:00
  • PHP 基金会来啦!
    Posted on 2022-10-08 17:40
  • 2018年云计算热词
    Posted on 2019-06-12 18:19