Problems related to the addition of identical elements in multi-dimensional arrays

Source: Internet
Author: User
This post was last edited by myths621 from 2013-03-1912: 04: 11 in the case of adding the same element in the multi-dimensional array. I took the five-dimensional array from redis and needed to process the addition, as follows: $ a & nbsp ;=& nbsp; array (0 & nbsp ;=& add identical elements in multi-dimensional arrays
This post was edited by myths621 at 12:04:11.
I need to process and add the five-dimensional array from redis, as shown below:

$a = array(

0 => array(

0=> array(

"date" => "201201",
"area"=> array(
12 => array(
click => "3",
traffic => "30"
)
)
),

1=> array(

"date" => "201202",
"area"=> array(
12 => array(
click => "8",
traffic => "40"
),
8=> array(
click => "5",
traffic => "50"
)
)
)

),

1 => array(

0=> array(

"date" => "201201",
"area"=> array(
12 => array(
click => "9",
traffic => "20"
),
8=> array(
click => "6",
traffic => "60"
)
)
),

1=> array(

"date" => "201202",
"area" => NULL
)
),


);



After data processing is expected, the values of click and traffic corresponding to the same key in the area are added on the premise of the same date, that is, the two large arrays 0 and 1, with date 201001, the values of click and traffic corresponding to 12 in area should be 6 and 60 corresponding to 12 and 50, 8, and so on. thanks for finding traversal or other methods that can be processed successfully ~! Multi-dimensional array
------ Solution --------------------
What is the final result? Paste it.
------ Solution --------------------
$ T = array ();
Foreach ($ a as $ k => $ v)
{
Foreach ($ v as $ k1 => $ v1)
{
$ T [0] [$ k1] ['Date'] = $ v1 ['Date'];
Foreach ($ v1 ['region'] as $ k2 => $ v2)
{
$ T [0] [$ k1] ['region'] [$ k2] ['click'] + = $ v2 ['click'];
$ T [0] [$ k1] ['area'] [$ k2] ['Traffic '] + = $ v2 ['Traffic'];
}
}
}
Echo"
";
Print_r ($ t );
Echo"
";


The result is as follows:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [date] => 201201
                    [area] => Array
                        (
                            [12] => Array
                                (
                                    [click] => 12

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.