php 用键名分组相加的二维数组实例函数
欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入 本文介绍一篇关于php 二维数组以某一键名进行分组相加的实例程序,如果是从数据库里取数据的时候大可以SELECT SUM(t_value),t_id FROM t_table GROUP BY t_id,但是如果是在php程序中处理类似的问
欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入
本文介绍一篇关于php 二维数组以某一键名进行分组相加的实例程序,如果是从数据库里取数据的时候大可以SELECT SUM(t_value),t_id FROM t_table GROUP BY t_id,但是如果是在php程序中处理类似的问题就稍微不太好办了,这里给个函数就是处理类似的问题
代码如下:
[php] view plaincopy
/* 函数功能:对二维数组以某一键名进行分组相加,返回新的二维数组
* 参数说明:$arr-源数组;$new_arr-相加后得到的新数组;$target_key-要分组的键名
*/ unity3d下载http://www.unitymanual.com
function add_array($arr, &$new_arr, $target_key) {
$num = count($new_arr); //计算新数组的大小,新数组也是二维的,这里计算的是第一维
for ($i = 0; $i
//循环新数组 (PS:^_^不错的php开发交流群:304224365,验证:csl,谢谢!)
//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);
以上是本文关于 php 用键名分组相加的二维数组实例函数,希望本文对广大php开发者有所帮助,感谢阅读本文。

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
