Php two-dimensional array group by key name adding instance function

Source: Internet
Author: User
Php two-dimensional array groups by key name. This article introduces an example program about php two-dimensional array groups by a key name, if the data is retrieved FROM the 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 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 );
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.