用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - php代码库

php 模拟mysql group by分组

2014-12-25 作者: php源代码大全举报

[php]代码库

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


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...