欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

php 模拟mysql group by分组

程序员文章站 2022-04-05 20:20:41
...

无详细内容 无 ?php$arrs = array( array('id'=3,'year'=2014,'month'=6,'day'=2,'name'='test1','money'=10233,'rebate'=111), array('id'=7,'year'=2017,'month'=6,'day'=2,'name'='test2','money'=12.36,'rebate'=100), array('id'=5,'year'=2014,'month'=

3,'year'=>2014,'month'=>6,'day'=>2,'name'=>'test1','money'=>10233,'rebate'=>111),
    array('id'=>7,'year'=>2017,'month'=>6,'day'=>2,'name'=>'test2','money'=>12.36,'rebate'=>100),
    array('id'=>5,'year'=>2014,'month'=>6,'day'=>2,'name'=>'test1','money'=>10233,'rebate'=>111),
    array('id'=>11,'year'=>2017,'month'=>6,'day'=>2,'name'=>'test2','money'=>12.36,'rebate'=>100),
   );
  
 
/*group by 后面的字段 可任意增加或减少*/
$groupBy= array('year','month','day'); 

/*order by 后面的字段,可任意增加或减少*/
$orderBy = array('money'=>'asc','id'=>'desc');

/*操作  格式: mysql内置函数=>字段名|结果别名 */
$option = array('sum'=>'money|sum_money','average'=>'money|average_money,rebate','count'=>'id|total','group_concat'=>'distinct:name|concat_name,rebate');
 
 
//Func::p(Func::handdleData($arrs, $groupBy, $orderBy, $option));
Func::p(Func::resultOrderBy($arrs,$orderBy));
 
class Func{
        static function p($data){
            echo '
';
            print_r($data);
        }
        static function handdleData($arrs, $groupBy, $orderBy, $option){
            $data = array();
            foreach($arrs as $index=>$arr){
                $groupKey = '';
                foreach($groupBy as $v){
                    $groupKey .= $arr[$v].'-';
                }
                $groupKey = trim($groupKey,'-');
                if(isset($data[$groupKey]))
                    array_push($data[$groupKey],$index);
                else
                    $data[$groupKey][]=$index;
            }
            $data = Func::resultGroupBy($arrs,$data,$groupBy,$option);
            $data = Func::resultOrderBy($data,$orderBy);
            return $data;
        }
        static function resultGroupBy($arrs,$temp,$groupBy,$option){
            $result = array();
            foreach($temp as $key=>$value){
                foreach($option as $k=>$f){
                    $parts = explode(',',$f);
                    $distinct = array();
                    foreach($parts as $part){
                        $exarr = explode('|',$part);
                        $filed = $exarr[0];
                        $aliasKey = isset($exarr[1]) ? $exarr[1] : '';
                        $aliasKey = !empty($aliasKey)?$aliasKey:$k.'_'.$filed;
                        if($k=='sum'){
                            $aliasValue = 0;
                            foreach($value as $v){
                                $aliasValue += $arrs[$v][$filed];
                            }
                        }elseif($k=='average'){
                            $aliasValue = 0;
                            foreach($value as $v){
                                $aliasValue += $arrs[$v][$filed];
                            }
                            $aliasValue = (float)$aliasValue/count($temp[$key]);
                        }elseif($k=='count'){
                            $aliasValue = count($value);
                        }elseif($k=='group_concat'){
                            if(strpos($filed,':')){
                                $distinct = explode(':', $filed);
                                $filed = $distinct[1];
                            }
                            $aliasValue = array();
                            foreach($value as $v){
                                $aliasValue[] = $arrs[$v][$filed];
                            }

                            $aliasValue = !empty($distinct) ? implode(',',array_unique($aliasValue)) : implode(',',$aliasValue);
                        }
                        $result[$key][$aliasKey] = $aliasValue;
                    }
                    
                }
            }
            foreach ($result as $key => &$value) {
                $key = explode('-', $key);
                for ($i = 0; $i  $value) {
                $temp = array();
                for($i = 0; $i