zoukankan      html  css  js  c++  java
  • 多维数组自定义排序

    function array_orderby()
    {
        $args = func_get_args();
        $data = array_shift($args);
        foreach ($args as $n => $field) {
            if (is_string($field)) {
                $tmp = array();
                foreach ($data as $key => $row)
                    $tmp[$key] = $row[$field];
                $args[$n] = $tmp;
                }
        }
        $args[] = &$data;
        call_user_func_array('array_multisort', $args);
        return array_pop($args);
    }
    
    
    $data[] = array('volume' => 67, 'edition' => 2);
    $data[] = array('volume' => 86, 'edition' => 1);
    $data[] = array('volume' => 85, 'edition' => 6);
    $data[] = array('volume' => 98, 'edition' => 2);
    $data[] = array('volume' => 86, 'edition' => 6);
    $data[] = array('volume' => 67, 'edition' => 7);
    
    
    $sorted = array_orderby($data, 'volume', SORT_DESC, 'edition', SORT_ASC);
      /**
         * @Author MR.Sui
         * @title [自定义排序]
         * @return mixed
         */
        public function Uorderby()
        {
            $args = func_get_args();
            $data = array_shift($args);
            foreach ($args as $n => $field)
           {
                if (is_string($field))
                {
                    $tmp = [];
                    array_walk($data,function($item,$key)use(&$tmp,$field){
                        $tmp[$key] = $item[$field];
                    });
                    $args[$n] = $tmp;
                }
            }
            $args[] = &$data;
           # sui($args);
            call_user_func_array('array_multisort', $args);
            return array_pop($args);
        }
    
     $menu =  $this->Uorderby($menu, 'sort', SORT_DESC);
    

      

  • 相关阅读:
    食谱
    食谱
    食谱
    无题
    重要通知
    幼儿食谱
    安卓应用开发常用代码
    安卓开发环境搭建
    《浪潮之巅》读书笔记——第11章 摩托罗拉
    预制redis数据
  • 原文地址:https://www.cnblogs.com/boundless-sky/p/7856875.html
Copyright © 2011-2022 走看看