Home > Backend Development > PHP Tutorial > PHP two-dimensional array grouping and adding by key name instance function_PHP tutorial

PHP two-dimensional array grouping and adding by key name instance function_PHP tutorial

WBOY
Release: 2016-07-13 10:25:37
Original
882 people have browsed it

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

Copy code The code is as follows:

/* 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);

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/824990.htmlTechArticle本文介绍一篇关于php 二维数组以某一键名进行分组相加的实例程序,如果是从数据库里取数据的时候大可以SELECT SUM(t_value),t_id FROM t_table G...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template