时间:2021-07-01 10:21:17 帮助过:48人阅读
<?php /** * 合并统计数据 * @param $key_column string 统计参照字段名 * @param $_list_column array 统计内容字段 统计字段默认值0 * @param mixed ...$_list_arr array 需合并统计内容 * @return array */ function statArrMergeHandle($key_column, $_list_column, ...$_list_arr) { //整理统计参照字段内容 $_total_key = array(); foreach ($_list_arr as $_arr) $_total_key = array_merge($_total_key, array_column($_arr, $key_column)); $_total_key = array_unique($_total_key); sort($_total_key); $_return_data = array(); //组合返回数组统计字段及内容,默认值0(根据情况自行设置默认值) $_modal_key = array(); foreach ($_list_column as $_column) $_modal_key[$_column] = 0; foreach ($_total_key as $key => $item) { $_arr_key = $_modal_key; $_arr_key[$key_column] = $item; foreach ($_list_arr as $_arr) {//遍历需合并数据结果 foreach ($_arr as $em) { if ($item == $em[$key_column]) { foreach ($_list_column as $_column) { //统计结果大于0,更新相应统计字段数据(判断数组内容是否存在) if ($em[$_column] > 0) $_arr_key[$_column] = $em[$_column]; } } } } $_return_data[$key] = $_arr_key; unset($_arr_key); } return $_return_data; } $score_a = array( array('grade' => '一年级', 'sum_a' => 20), array('grade' => '二年级', 'sum_a' => 21), array('grade' => '三年级', 'sum_a' => 15), array('grade' => '四年级', 'sum_a' => 3), array('grade' => '五年级', 'sum_a' => 14) ); $score_b = array( array('grade' => '一年级', 'sum_b' => 21), array('grade' => '二年级', 'sum_b' => 14), array('grade' => '四年级', 'sum_b' => 40), array('grade' => '五年级', 'sum_b' => 12), array('grade' => '六年级', 'sum_b' => 25), ); $score_c = array( array('grade' => '一年级', 'sum_c' => 45), array('grade' => '三年级', 'sum_c' => 24), array('grade' => '四年级', 'sum_c' => 5), array('grade' => '六年级', 'sum_c' => 12) ); $score_d = array( array('grade' => '一年级', 'sum_d' => 12), array('grade' => '二年级', 'sum_d' => 45), array('grade' => '六年级', 'sum_d' => 12) ); $_last_data = statArrMergeHandle('grade', array('sum_a', 'sum_b', 'sum_c', 'sum_d'), $score_a, $score_b, $score_c, $score_d);
最终结果:
相关教程:PHP视频教程
以上就是PHP如何实现统计数据合并的详细内容,更多请关注Gxlcms其它相关文章!