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

php 模拟mysql group by分组

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-06 19:35:15
Original
1014 people have browsed it

无详细内容 无 ?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;
        }
}
Copy after login
source:php.cn
Statement of this Website
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template