PHP two-dimensional array Add the instance function _php instance with the key name group

Source: Internet
Author: User
This article describes a PHP two-dimensional array with a key to group add the instance program, if the data from the database can be a large select SUM (t_value), t_id from T_table Group by t_id, But if you're dealing with a similar problem in a PHP program that's a bit tricky, here's a function to handle a similar problem


Copy Code code as follows:



<?php


/* Function function: The two-dimensional array is added with a key name to return the new two-dimensional array


* Parameter Description: $arr-source Array, $new _arr-added after the new array; $target _key-the key name to be grouped


 */


function Add_array ($arr, & $new _arr, $target _key) {


$num = count ($new _arr); Computes the size of the new array, which is also two-dimensional, and this computes the first dimensional


for ($i = 0; $i < $num; $i + +) {


//Cyclic new array

The
//if block mainly determines whether the current group's key name already exists in the new array, avoiding duplication


//Because the function is called circularly, and the new array may have more than 1 elements, you must compare each element in the new array,

The element of the
//new array is a one-dimensional array $i the dynamic comparison of the group key names in the new two-dimensional array


if ($arr [$target _key]!= $new _arr[$i [$target _key]) {//Determine if the grouping key name in the new array is equal to the group key name in the current source array


$cmp _num++; If not equal, the number of comparisons is increased by 1


} else {//if equal, indicates that the current group key name already exists


$tar _exist = true; The setting exists with the identity of true


$tar _key = $i; Returns the numeric index of the current group key name in the new array


break; Jump out of the loop


        }


    }


///If the comparison is the same as the new array size, the current group key name is not in the new array, and the setting has an ID of 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) {//group key name corresponding to the element value is not added


$new _arr[$tar _key][$key]+= $value; The remaining element values are added


            }


        }


} else {


//If the group key name does not exist


//sets the new group key name and adds the array elements of the group

The first dimension of the
//new array uses $num parameters to distinguish the order of the current group


//Because $num is actually the new array, the number of key names is grouped and is starting at 0, so the index of the new grouping in the new array is directly $num.


//Without the need for $num+1


$new _arr[$num] [$target _key] = $arr [$target _key];


foreach ($arr as $key => $value) {


if ($key!= $target _key) {//group key name corresponding to the element value is not added


$new _arr[$num] [$key]+= $value; The remaining element values are added


            }


        }


    }


}


$arr = Array (


Array (' group_id ' =>, ' Team_price ' => 88.00, ' Satopay_price ' => 85.00, ' team_id ' => 348, ' origin ' = > 440, ' Gain ' => 14.45, ' quantity ' => 5),


Array (' group_id ' =>, ' team_price ' => 12.00, ' satopay_price ' => 11.00, ' team_id ' => 344, ' origin ' => 36, ' Gain ' => 2.76, ' Quantity ' => 3),


Array (' group_id ' =>, ' Team_price ' => 4.99, ' Satopay_price ' => 4.60, ' team_id ' => 335, ' origin ' =&G T 4.99, ' Gain ' => 0.31915, ' quantity ' => 1),


Array (' group_id ' =>, ' team_price ' => 12.00, ' satopay_price ' => 11.00, ' team_id ' => 344, ' origin ' = >, ' Gain ' => 1.84, ' quantity ' => 2),


Array (' group_id ' =>, ' team_price ' => 13.00, ' Satopay_price ' => 11.00, ' team_id ' => 344, ' origin ' = >, ' Gain ' => 1.84, ' quantity ' => 2),


);


$new _arr = Array ();


foreach ($arr as $key => $value) {


Add_array ($value, & $new _arr, ' group_id '); Here we press group_id to group add


}


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.