PHP two-dimensional array with key name grouping add instance function _php instance

Source: Internet
Author: User
This article introduces a PHP two-dimensional array with a key name for the group add the instance program, if the data from the database when the large can select SUM (t_value), t_id from the T_table group by t_id, But if you're dealing with a similar problem in a PHP program, it's a little tricky, and here's a function to deal with a similar problem.
Copy CodeThe code is as follows:
/* Function: Adds a two-dimensional array to a key name and returns a new two-dimensional array
* Parameter Description: $arr-source array; The new array $new _arr-, and the key name to be grouped $target _key-
*/
function Add_array ($arr, & $new _arr, $target _key) {
$num = count ($new _arr); To calculate the size of the new array, the new array is also two-dimensional, where the first dimension is computed
for ($i = 0; $i < $num; $i + +) {
Looping a new array
The If block primarily determines whether the currently grouped key name already exists in the new array, avoiding duplication
Since the function is called by loop, 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 dynamically comparing the grouping key names in the new two-dimensional array
if ($arr [$target _key]! = $new _arr[$i] [$target _key]) {//Determine if the grouping key name in the new array is equal to the grouping key name in the current source array
$CMP _num++; If not equal, the number of comparisons is increased from 1
} else {//if equal, indicates that the current grouping key name already exists
$tar _exist = true; Set presence ID to True
$tar _key = $i; Returns the numeric index of the current grouping key name in the new array
Break Jump out of the loop
}
}
If the number of comparisons is the same as the new array size, the current grouping key name is not in the new array, and the setting exists identified as false
if ($cmp _num = = $num)
$tar _exist = false;
if ($tar _exist) {//If the grouping key name already exists, add the array elements of the grouping
foreach ($arr as $key = = $value) {
if ($key! = $target _key) {//group key names correspond to element values not added
$new _arr[$tar _key][$key]+= $value; Add the remaining element values
}
}
} else {
If the grouping key name does not exist
Sets the new grouping key name and adds the array elements of the grouping
The first dimension of the new array uses the $num parameter to distinguish the order of the current grouping
Since $num is actually the number of key names grouped in the new array, and is starting from 0, the new grouping in the new array will be indexed directly with $num.
Without the need of $num+1
$new _arr[$num] [$target _key] = $arr [$target _key];
foreach ($arr as $key = = $value) {
if ($key! = $target _key) {//group key names correspond to element values not added
$new _arr[$num] [$key]+= $value; Add the remaining element values
}
}
}
}
$arr = Array (
Array (' group_id ' = +, ' team_price ' = = 88.00, ' satopay_price ' + 85.00, ' team_id ' = 348, ' origin ' = 440, ' Gain ' and 14.45, ' quantity ' + 5),
Array (' group_id ' = +, ' team_price ' = = 12.00, ' satopay_price ' + 11.00, ' team_id ' = 344, ' origin ' = 36, ' Gain ' and 2.76, ' quantity ' + 3),
Array (' group_id ' = +, ' team_price ' = = 4.99, ' Satopay_price ' + 4.60, ' team_id ' = 335, ' origin ' = 4.99, ' Gain ' and 0.31915, ' quantity ' + 1),
Array (' group_id ' = +, ' team_price ' = = 12.00, ' satopay_price ' + 11.00, ' team_id ' = 344, ' origin ' = 24, ' Gain ' and 1.84, ' quantity ' + 2),
Array (' group_id ' = +, ' team_price ' = = 13.00, ' satopay_price ' + 11.00, ' team_id ' = 344, ' origin ' = 24, ' Gain ' and 1.84, ' quantity ' + 2),
);
$new _arr = Array ();
foreach ($arr as $key = = $value) {
Add_array ($value, & $new _arr, ' group_id '); Here we're grouping 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.