Welcome to the Linux community forum and interact with 2 million technical staff. This article introduces an instance program for grouping and adding two-dimensional php arrays by one click, you can select tsum (t_value) and t_idFROMt_tableGROUPBYt_id When retrieving data from the database, but process similar questions in the php program.
Welcome to the Linux community forum and interact with 2 million technical staff> This article introduces an instance program for grouping and adding two-dimensional php arrays by one click, select sum (t_value), t_id FROM t_table group by t_id if the data is retrieved FROM the database, but if the data is processed in a php program
Welcome to the Linux community forum and interact with 2 million technicians>
This article introduces an example program for adding a php two-dimensional array to a group by one click. If you want to retrieve data from the database, you can select sum (t_value ), t_id FROM t_table group by t_id, but it is not easy to handle similar problems in php programs. Here, a function is used to handle similar problems.
The Code is as follows:
[Php] view plaincopy
/* 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
*/Unity3d download http://www.unitymanual.com
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 through the new array (PS: ^ _ ^ good php development group: 304224365, verification: csl, thank you !)
// 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 );
The above is a two-dimensional array instance function for php grouping by key name. I hope this article will be helpful to php developers. Thank you for reading this article.