


PHP two-dimensional array grouping and adding by key name instance function_PHP tutorial
This article introduces an example program about PHP two-dimensional array grouping and adding with a certain key name. If you are fetching data from the database, you can SELECT SUM(t_value), t_id FROM t_table GROUP BY t_id, but if you are fetching data from the database, It is a little more troublesome to deal with similar problems in PHP programs. Here is a function to handle similar problems
/* Function: Group and add two-dimensional arrays with a certain key name, and return a new two-dimensional array
* Parameter description: $arr-source array ; $new_arr - the new array obtained after addition; $target_key - the key name to be grouped
*/
function add_array($arr, &$new_arr, $target_key) {
$num = count( $new_arr); //Calculate the size of the new array. The new array is also two-dimensional. The first dimension is calculated here
for ($i = 0; $i < $num; $i++) {
//Loop the new array
//The if block mainly determines whether the key name of the current group already exists in the new array to avoid duplication
//Since this function is called in a loop, the new array may have more than 1 element, so each element in the new array must be compared,
//The elements of the new array are a one-dimensional array, $i dynamically compares the grouping key names in the new two-dimensional array
if ($arr[$target_key] != $new_arr[$i][$target_key]) {//Determine whether the grouping key name in the new array is equal to the grouping key name in the current source array
; //If not equal, the number of comparisons will be incremented by 1
} else {//If equal, the current grouping key name already exists
$tar_exist = true; // = $i; //Return the numeric index of the current grouping key in the new array
break; //Jump out of the loop
}
}
//If the number of comparisons is the same as the size of the new array, explain The current grouping key is not in the new array, set the existence flag to false
if ($cmp_num == $num)
$tar_exist = false;
if ($tar_exist) {//If the grouping key has been exists, add the array elements of the group
foreach ($arr as $key => $value) {
if ($key != $target_key) {//The element value corresponding to the grouping key name Do not add
, The grouping key does not exist
//Set a new grouping key and add the array elements of the group
//The first dimension of the new array uses the $num parameter to determine the order of the current grouping
// Since $num is actually the number of key name groups in the new array, and it starts from 0, so the index of the new group in the new array can be directly used as $num,
Requires $num+1
$new_arr[$num][$target_key] = $arr[$target_key];
foreach ($arr as $key => $value) {
if ($key != $target_key) {//The element values corresponding to the grouping key names are not added
. 🎜>
$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

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

Alipay PHP...

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,

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

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.

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...
