PHP实现分组 类似mysqlgroupby+in读取 反正都要读取 那就用PHP来分组吧
/** * @description:根据数据 * @param {dataArr:需要分组的数据;keyStr:分组依据} * @return: */ protected function dataGroup($dataArr, $keyStr) { $newArr = []; foreach ($dataArr as $k => $val) { $newArr[$val[$keyStr]][] = $val; } return $newArr; }
$array = array( array('name' => 'zhangsan', 'chinese' => '89', 'math' => '95', 'english' => '88'), array('name' => 'zhangsan', 'chinese' => '11', 'math' => '816', 'english' => '90'), array('name' => 'lisi', 'chinese' => '3', 'math' => '286', 'english' => '902'), array('name' => 'lisi', 'chinese' => '921', 'math' => '836', 'english' => '90'), array('name' => 'lisi', 'chinese' => '291', 'math' => '846', 'english' => '290'), ); $list=$this->dataGroup($array,'name'); print_r($list); exit();
输出如下
Array ( [zhangsan] => Array ( [0] => Array ( [name] => zhangsan [chinese] => 89 [math] => 95 [english] => 88 ) [1] => Array ( [name] => zhangsan [chinese] => 11 [math] => 816 [english] => 90 ) ) [lisi] => Array ( [0] => Array ( [name] => lisi [chinese] => 3 [math] => 286 [english] => 902 ) [1] => Array ( [name] => lisi [chinese] => 921 [math] => 836 [english] => 90 ) [2] => Array ( [name] => lisi [chinese] => 291 [math] => 846 [english] => 290 ) ) )
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/19010.html