zoukankan      html  css  js  c++  java
  • php二维数组排序

    场景:数据来自于excel,读取出来是二维数组,需要按照指定字段排序,类似于sql的order by功能,代码如下:
    
    ```
    class demo {
        public function test() {
            $arr = array(
                array(
                    'power' => 380,
                    'day' => 1,
                    'where' => 1,
                ),
                array(
                    'power' => 380,
                    'day' => 2,
                    'where' => 1,
                ),
                array(
                    'power' => 400,
                    'day' => 1,
                    'where' => 1,
                ),
                array(
                    'power' => 380,
                    'day' => 3,
                    'where' => 1,
                ),
                array(
                    'power' => 400,
                    'day' => 2,
                    'where' => 1,
                ),
                array(
                    'power' => 380,
                    'day' => 8,
                    'where' => 1,
                ),
                array(
                    'power' => 380,
                    'day' => 8,
                    'where' => 2,
                ),
                array(
                    'power' => 500,
                    'day' => 1,
                    'where' => 1,
                ),
                array(
                    'power' => 380,
                    'day' => 11,
                    'where' => 1,
                ),
                array(
                    'power' => 380,
                    'day' => 5,
                    'where' => 1,
                ),
            );
    
            $orderArr = array(
                'power' => 'asc',
                'day' => 'asc',
                'where' => 'asc',
            );
    
            // 调用排序函数
            $this->sortBySelf($arr, $orderArr);
    
            var_dump($arr);die;
        }
    
    
        /**
         * 自定义排序.
         *
         * @param array $arr   要排序的数组.
         * @param array $order 排序规则.
         *
         * @return array.
         */
        private function sortBySelf(&$arr = array(), $order = array()) {
            if (empty($order)) {
                return $arr;
            }
    
            $sortFuncNameArr = array(
                'desc' => 'descSortByKey',
                'asc' => 'ascSortByKey',
            );
    
            $index = array();
    
            $i = 0;
            $keys = array_keys($order);
            foreach ($order as $key => $value) {
                if ($i == 0) {
                    $funcName = $sortFuncNameArr[$value];
                    usort($arr, self::$funcName($key));
                    // 定义第一个字段排序完的index
                    $index = $this->setIndex($arr, array_slice($keys,0,$i+1));
                    $i++;
                    continue;
                }
    
                if (empty($index)) {
                    break;
                }
    
                foreach ($index as $beSortKey => $range) {
                    if (count($range) <= 1) {
                        continue;
                    }
    
                    $tmpSortArr = array_slice($arr, $range[0], count($range), true);
    
                    $funcName = $sortFuncNameArr[$value];
                    usort($tmpSortArr, self::$funcName($key));
    
                    array_splice($arr, $range[0], count($range), $tmpSortArr);
    
                    // 定义index
                    $index = $this->setIndex($arr, array_slice($keys,0,$i+1));
                }
    
                $i++;
            }
    
        }
    
        /**
         * 每次排序完设置分区.
         *
         * @param array $arr       要排序的数组.
         * @param array $beSortKey 已经排序完的字段.
         *
         * @return array.
         */
        private function setIndex($arr = array(), $beSortKey = array()) {
            $index = array();
    
            foreach ($arr as $arrKey => $value) {
                $real = '';
                for ($i = 0; $i < count($beSortKey); $i++) {
                    $real .= $value[$beSortKey[$i]];
                }
                $index[$real][] = $arrKey;
            }
    
            return $index;
        }
    
        /**
         * 倒序回调函数.
         *
         * @param string $key 要排序的KEY.
         *
         * @return Closure.
         */
        public static function descSortByKey($key = '') {
            return function ($a, $b) use ($key) {
                if ($a[$key] == $b[$key])
                    return 0;
                return ($a[$key] > $b[$key]) ? -1 : 1;
            };
        }
    
        /**
         * 正序回调函数.
         *
         * @param string $key 要排序的KEY.
         *
         * @return Closure.
         */
        public static function ascSortByKey($key = '') {
            return function ($a, $b) use ($key) {
                if ($a[$key] == $b[$key])
                    return 0;
                return ($a[$key] < $b[$key]) ? -1 : 1;
            };
        }
    
    }
    ```
  • 相关阅读:
    26_为什么我的删除刷新没有办法删除动态添加的数据呢?
    075_not in (null):这代表是什么意思呢?
    074_form表单中的value值
    025_回调函数没有遍历出数据
    073_模糊查询
    072_jsp追加与刷新数据
    071_为什么要catch return.setCode()?
    070_jstl中的三目表达式
    069_都是查询语句时得事物?
    打印周报模板
  • 原文地址:https://www.cnblogs.com/anyeshe/p/5750490.html
Copyright © 2011-2022 走看看