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