Home >Backend Development >PHP Tutorial >PHP two-dimensional array is grouped and added by a certain key_PHP tutorial
This article will introduce to you an example program of grouping and adding two-dimensional arrays with a certain key name in PHP. I hope this tutorial will be helpful to you.
As mentioned, 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 dealing with similar problems in a php program, it will be a little more troublesome. Here is a function to handle similar problems
The code is as follows | Copy code |
/* 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-new array obtained after addition; $target_key-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, and the first dimension is calculated here for ($i = 0; $i < $num; $i++) { //Loop 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, and the new array may have more than 1 element, 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 $cmp_num++; //If not equal, the number of comparisons will increase by 1 } else {//If equal, it means that the current grouping key name already exists $tar_exist = true; //Set the existence flag to true $tar_key = $i; //Return the numeric index of the current grouping key name in the new array break; //break out of the loop } } //If the number of comparisons is the same as the size of the new array, it means that the current grouping key name is not in the new array, set the existence flag to 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) {//The element values corresponding to the grouping key names do not add up $new_arr[$tar_key][$key]+=$value; //Add the remaining element values } } } else { //If the group key name does not exist //Set a new grouping key name and add the array elements of the grouping //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, the index of the new group in the new array can be directly $num, //No need for $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 do not add up $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); |