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