The PHP two-dimensional array is added together by a key name

Source: Internet
Author: User

As the title, if the data from the database when the big can

SELECT SUM (T_value), t_id from T_table GROUP by t_id

But if you're dealing with a similar problem in a PHP program, it's a bit of a hassle, and here's a function to deal with a similar problem.

The code is as follows Copy Code

<?php

/* Function function: A two-dimensional array is added with a key name, and a new two-dimensional array is returned.

* Parameter Description: $arr-source Array, $new _arr-added after the new array; $target _key-the key name to be grouped

*/

function Add_array ($arr, & $new _arr, $target _key) {

$num = count ($new _arr); Computes the size of the new array, which is also two-dimensional, where the first dimension is computed

for ($i = 0; $i < $num; $i + +) {

Loop new Array

The If block mainly determines whether the current group's key name already exists in the new array, avoiding duplication

Because the function is called circularly, and the new array may have more than 1 elements, you must compare each element in the new array.

The element of the new array is a one-dimensional array $i the group key names in the dynamic comparison of the new two-dimensional array

if ($arr [$target _key]!= $new _arr[$i] [$target _key]) {//To determine whether the grouping key names in the new array are equal to the group key names in the current source array

$CMP _num++; If not equal, the number of comparisons is increased by 1

else {//if equal, indicates that the current group key name already exists

$tar _exist = true; Set presence ID to True

$tar _key = $i; Returns the numeric index of the current group key name in the new array

Break Jump out of the loop

}

}

If the comparison is the same as the new array size, the current group key name is not in the new array, and the setting exists with the ID false

if ($cmp _num = = $num)

$tar _exist = false;

if ($tar _exist) {//If the group key name already exists, add the array elements of the group

foreach ($arr as $key => $value) {

if ($key!= $target _key) {//group key name corresponding to the element value does not add

$new _arr[$tar _key][$key]+= $value; The remaining element values are added

}

}

} else {

If the group key name does not exist

Sets the new grouping key name and adds the array elements of the group

The first dimension of the new array uses the $num parameter to distinguish the order of the current group

Since $num is actually the number of key names that are grouped in the new array, and is starting at 0, the index of the new grouping in the new array is directly $num.

Without the need for $num+1.

$new _arr[$num] [$target _key] = $arr [$target _key];

foreach ($arr as $key => $value) {

if ($key!= $target _key) {//group key name corresponding to the element value does not add

$new _arr[$num] [$key]+= $value; The remaining element values are added

}

}

}

}

$arr = Array (

Array (' group_id ' =>, ' Team_price ' => 88.00, ' Satopay_price ' => 85.00, ' team_id ' => 348, ' origin ' => 440, ' Gain ' => 14.45, ' quantity ' => 5),

Array (' group_id ' =>, ' team_price ' => 12.00, ' satopay_price ' => 11.00, ' team_id ' => 344, ' origin ' => 36, ' Gain ' => 2.76, ' Quantity ' => 3),

Array (' group_id ' =>, ' Team_price ' => 4.99, ' Satopay_price ' => 4.60, ' team_id ' => 335, ' origin ' => 4.99, ' Gain ' => 0.31915, ' quantity ' => 1),

Array (' group_id ' =>, ' team_price ' => 12.00, ' satopay_price ' => 11.00, ' team_id ' => 344, ' origin ' => 24, ' Gain ' => 1.84, ' quantity ' => 2),

Array (' group_id ' =>, ' 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 '); Here we add the group by group_id

}

Var_dump ($new _arr);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.