Simulate mysqlgroupby grouping in php

Source: Internet
Author: User
No details? Php $ arrsarray (array (id3, year2014, month6, day2, nametest1, money10233, Hangzhou), array (id7, year2017, month6, day2, nametest2, money12.36, rebate100 ), array (id5, year2014, month

No details? Php $ arrs = 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, 'reb' = 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, 'rebin' => 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),);/* the field after group by can be arbitrarily added or removed */$ groupBy = array ('Year ', 'month', 'day');/* the field after order by, which can be arbitrarily added or removed */$ orderBy = array ('money' => 'asc ', 'id' => 'desc');/* operation format: mysql built-in function => Field name | result alias */$ 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 (':', $ fi Led); $ 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 = ''; f Oreach ($ 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
   
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.