For example, if the data is retrieved from the database, you can use the following SQLSELECTSUM (t_value), t_idFROMt_tableGROUPBYt_id, but if it is in the php program
For example, you can use the following SQL statement to retrieve data from a database:
Select sum (t_value), t_id FROM t_table group by t_id
However, it would be a little troublesome to handle similar problems in php programs. here, a function is used to handle similar problems. The instance code is as follows:
-
- /* Function: Groups and adds two-dimensional arrays with one click, and returns 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. The first dimension is calculated here.
- For ($ I = 0; $ I <$ num; $ I ++ ){
- // Loop the new array
- // If block mainly checks whether the key name of the current group already exists in the new array to avoid repeated
- // Since this function is called cyclically, and the new array may have more than one element, each element in the new array must be compared,
- // The element of the new array is a one-dimensional array. $ I dynamically compares the group key names in the new two-dimensional array.
- If ($ arr [$ target_key]! = $ New_arr [$ I] [$ target_key]) {// checks whether the group key name in the new array matches the group key name in the current source array.
- $ Cmp_num ++; // if not equal, the number of comparisons increases by 1
- } Else {// if they are equal, the current group key name already exists.
- $ Tar_exist = true; // you can specify true as an existing identifier.
- $ Tar_key = $ I; // returns the numeric index of the current group key name in the new array.
- Break; // skip loop
- }
- }
- // If the number of comparisons is the same as the size of the new array, it means that the current group key name is not in the new array, and the existing identifier is set 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 group key name are not added.
- $ New_arr [$ tar_key] [$ key] + = $ value; // add the remaining element values.
- }
- }
- } Else {
- // If the group key name does not exist
- // Set a new group key name and add the array elements of the group
- // The first dimension of the new array uses the $ num parameter to identify the order of the current group.
- // Because $ num is actually the number of key group names in the new array and starts from 0, you can directly use $ num for the index of the new group in the new array,
- // $ Num + 1 is not required
- $ New_arr [$ num] [$ target_key] = $ arr [$ target_key];
- Foreach ($ arr as $ key => $ value ){
- If ($ key! = $ Target_key) {// The element values corresponding to the group key name are not added.
- $ New_arr [$ num] [$ key] + = $ value; // add the remaining element values.
- }
- }
- }
- }
- $ 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'); // Here we add groups by group_id
- }
- Var_dump ($ new_arr );