Php two-dimensional array instance function grouped by key name

Source: Internet
Author: User
This article introduces an instance program for grouping and adding a php two-dimensional array with a single key name. if it is used to retrieve data from the database, it can be SELECTSUM (t_value), t_idFROMt_tableGROUPBYt_id, however, if you are dealing with similar problems in the php program, this article will introduce a php two-dimensional array to group and add an instance program with a single click 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 difficult to handle similar problems in php programs. here, a function is used to handle similar problems. The code is as follows:
 $ 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, 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 in the new array, the number of key group names, starting from 0. Therefore, you can directly use $ num to index the new group in the new array, // $ 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 group key name are not added $ new_arr [$ num] [$ key] + = $ value; // add the remaining element values }}}$ arr = 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.

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.