Home >php教程 >php手册 >php 模拟mysql group by分组

php 模拟mysql group by分组

WBOY
WBOYOriginal
2016-06-06 19:35:151002browse

无详细内容 无 ?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'=

<?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'=>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 '<pre />';
            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 < count($groupBy); $i++) {
                    $value[$groupBy[$i]] = $key[$i];
                }
            }
            return array_values($result);
        }
        static function resultOrderBy($arrs,$orderBy){
            $orderArr = array();
            $orderType = array();
            $sortRule = '';
            foreach ($orderBy as $key => $value) {
                $temp = array();
                for($i = 0; $i < count($arrs); $i++){
                    $temp[] = $arrs[$i][$key];
                }
                $orderArr[] = $temp;
                $orderType[] = $value == 'asc' ? SORT_ASC : SORT_DESC;
            }
            for($i=0; $i<count($orderBy); $i++) {
                $sortRule .= '$orderArr['.$i.'],' . $orderType[$i].',' ;
            }
            //echo 'array_multisort('.$sortRule.'$arrs);';
            eval('array_multisort('.$sortRule.'$arrs);');
            return $arrs;
        }
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn