PHP实现-二维数组排序

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

  • 分布式服务限流
    Posted on 2020-02-07 18:57
  • 有状态服务VS无状态服务
    Posted on 2020-02-07 18:18
  • 企业级PAAS云平台几个关键问题和挑战
    Posted on 2019-06-12 18:33
  • MySQL 单库后期分库策略
    Posted on 2019-08-19 14:31
  • Redis七大经典问题
    Posted on 2021-05-27 11:14
  • 关于HTTPS的五大误区
    Posted on 2020-02-02 01:10
  • PHP实现精确发布时间
    Posted on 2018-12-06 21:00
  • 为什么要测量尾部延迟
    Posted on 2020-09-18 10:34