• PHP实现分组 类似mysql groupby+in读取


    PHP实现分组 类似mysqlgroupby+in读取 反正都要读取 那就用PHP来分组吧

        /**
         * @description:根据数据
         * @param {dataArr:需要分组的数据;keyStr:分组依据}
         * @return:
         */
        protected function dataGroup($dataArr, $keyStr)
        {
            $newArr = [];
            foreach ($dataArr as $k => $val) {
      
                $newArr[$val[$keyStr]][] = $val;
            }
            return $newArr;
        }
      $array = array(
                array('name' => 'zhangsan', 'chinese' => '89', 'math' => '95', 'english' => '88'),
                array('name' => 'zhangsan', 'chinese' => '11', 'math' => '816', 'english' => '90'),
                array('name' => 'lisi', 'chinese' => '3', 'math' => '286', 'english' => '902'),
                array('name' => 'lisi', 'chinese' => '921', 'math' => '836', 'english' => '90'),
                array('name' => 'lisi', 'chinese' => '291', 'math' => '846', 'english' => '290'),
            );
            $list=$this->dataGroup($array,'name');
            print_r($list);
            exit();

    输出如下

    Array
    (
        [zhangsan] => Array
            (
                [0] => Array
                    (
                        [name] => zhangsan
                        [chinese] => 89
                        [math] => 95
                        [english] => 88
                    )
    
                [1] => Array
                    (
                        [name] => zhangsan
                        [chinese] => 11
                        [math] => 816
                        [english] => 90
                    )
    
            )
    
        [lisi] => Array
            (
                [0] => Array
                    (
                        [name] => lisi
                        [chinese] => 3
                        [math] => 286
                        [english] => 902
                    )
    
                [1] => Array
                    (
                        [name] => lisi
                        [chinese] => 921
                        [math] => 836
                        [english] => 90
                    )
    
                [2] => Array
                    (
                        [name] => lisi
                        [chinese] => 291
                        [math] => 846
                        [english] => 290
                    )
    
            )
    
    )
  • 相关阅读:
    VSCode中按ESLint规则格式化Javascript代码
    VSCode设置资源管理器字体大小
    Windows下利用安装压缩包安装MySQL
    Windows部署Apache 2.4.46及PHP 8.0.3
    npm设置国内镜像
    IDEA运行Tomcat输出信息乱码
    深入理解jvm虚拟机读书笔记-Java内存区域与内存溢出异常
    Navicat Premium
    mysql安装
    ElasticSearch 基础
  • 原文地址:https://www.cnblogs.com/newmiracle/p/13884241.html
Copyright © 2020-2023  润新知