Php 2D arrays are grouped and added by a single-click name.

Source: Internet
Author: User

This article will introduce you to an instance program for grouping and adding two-dimensional php arrays with a single click. I hope this tutorial will be helpful to you.

For example, if you want to retrieve data from the database, you can

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: Copy code

<? Php

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

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.