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;
        };
    }

}
```
相關文章
相關標籤/搜索