php二维数组以键名进行分组相加的实例程序
本文介绍一篇关于php 二维数组以某一键名进行分组相加的实例程序,如果是从数据库里取数据的时候大可以SELECT SUM(t_value),t_id FROM t_table GROUP BY t_id,但是如果是在php程序中处理类似的问题就稍微麻烦点了,,这里给个函数就是处理类似的问题
复制代码 代码如下:
/* 函数功能:对二维数组以某一键名进行分组相加,返回新的二维数组
* 参数说明:$arr-源数组;$new_arr-相加后得到的新数组;$target_key-要分组的键名
*/
function add_array($arr, &$new_arr, $target_key) {
$num = count($new_arr); //计算新数组的大小,新数组也是二维的,这里计算的是第一维
for ($i = 0; $i //循环新数组
//if块主要判断当前分组的键名是否已经存在于新数组中,避免重复
//由于该函数是被循环调用的,而新数组可能有多于1个的元素,所以必须对新数组中的每一个元素都进行比较,
//新数组的元素是一个一维数组,$i动态的比较新的二维数组中的分组键名
if ($arr[$target_key] != $new_arr[$i][$target_key]) {//判断新数组中的分组键名是否跟当前源数组中的分组键名相等
$cmp_num++; //如果不相等,比较次数自增1
} else {//如果相等,说明当前分组键名已经存在
$tar_exist = true; //设置存在标识为true
$tar_key = $i; //返回当前分组键名在新数组中的数字索引
break; //跳出循环
}
}
//如果比较次数跟新数组大小一样,说明当前分组键名不在新数组中,设置存在标识为false
if ($cmp_num == $num)
$tar_exist = false;
if ($tar_exist) {//如果分组键名已经存在,对该分组的数组元素进行相加
foreach ($arr as $key => $value) {
if ($key != $target_key) {//分组键名对应的元素值不相加
$new_arr[$tar_key][$key]+=$value; //其余的元素值进行相加
}
}
} else {
//如果分组键名不存在
//设置新的分组键名,并对该分组的数组元素进行相加
//新数组的第一维使用$num参数来分辨当前分组的秩序
//由于$num实际上就是新数组中,按键名分组的个数,并且是从0开始,所以新的分组在新数组中的索引直接用$num即可,
//而不须要$num+1
$new_arr[$num][$target_key] = $arr[$target_key];
foreach ($arr as $key => $value) {
if ($key != $target_key) {//分组键名对应的元素值不相加
$new_arr[$num][$key]+=$value; //其余的元素值进行相加
}
}
}
}
$arr = array(
array('group_id' => 13, 'team_price' => 88.00, 'satopay_price' => 85.00, 'team_id' => 348, 'origin' => 440, 'gain' => 14.45, 'quantity' => 5),
array('group_id' => 13, 'team_price' => 12.00, 'satopay_price' => 11.00, 'team_id' => 344, 'origin' => 36, 'gain' => 2.76, 'quantity' => 3),
array('group_id' => 14, 'team_price' => 4.99, 'satopay_price' => 4.60, 'team_id' => 335, 'origin' => 4.99, 'gain' => 0.31915, 'quantity' => 1),
array('group_id' => 14, 'team_price' => 12.00, 'satopay_price' => 11.00, 'team_id' => 344, 'origin' => 24, 'gain' => 1.84, 'quantity' => 2),
array('group_id' => 15, 'team_price' => 13.00, 'satopay_price' => 11.00, 'team_id' => 344, 'origin' => 24, 'gain' => 1.84, 'quantity' => 2),
);
$new_arr = array();
foreach ($arr as $key => $value) {
add_array($value, &$new_arr, 'group_id'); //这里我们按group_id进行分组相加
}
var_dump($new_arr);

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6
Visual web development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),