Home > Backend Development > PHP Tutorial > 多维数组中相同元素相加问题

多维数组中相同元素相加问题

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-23 14:03:14
Original
858 people have browsed it

首次发帖,请假高手
我从redis中取出五维数组需要处理相加,如下

$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			)		),			);
Copy after login


希望数据处理后,相同date前提下,area里相同的key所对应的click和traffic值相加,即0和1这两个大数组,date为201001的,area中12所对应的click和traffic处理后应该是12和50,8对应的是6和60,其他类推,求遍历或其他能处理成功的方法,感激不尽~!


回复讨论(解决方案)

最后结果是怎么样的? 贴一下。

结果如下,感谢关注

$a = array(             0 => array(             0=> array(                             "date" => "201201",                "area"=> array(                    12 => array(                        click => "12",                        traffic => "50"                    ) ,					8=> array(                        click => "6",                        traffic => "60"                    )                   )            ),                 1=> array(                             "date" => "201202",                "area"=> array(                    12 => array(                        click => "8",                        traffic => "40"                    ),                    8=> array(                        click => "5",                        traffic => "50"                    )                )            )                         )              );
Copy after login

$t = array();foreach ($a as $k=>$v){	foreach ($v as $k1=>$v1)	{		$t[0][$k1]['date'] = $v1['date'];		foreach ($v1['area'] as $k2=>$v2)		{			$t[0][$k1]['area'][$k2]['click'] += $v2['click'];			$t[0][$k1]['area'][$k2]['traffic'] += $v2['traffic'];		}	}}echo "<pre class="brush:php;toolbar:false">";print_r($t);echo "<pre class="brush:php;toolbar:false">";
Copy after login


结果如下:
Array(    [0] => Array        (            [0] => Array                (                    [date] => 201201                    [area] => Array                        (                            [12] => Array                                (                                    [click] => 12                                    [traffic] => 50                                )                            [8] => Array                                (                                    [click] => 6                                    [traffic] => 60                                )                        )                )            [1] => Array                (                    [date] => 201202                    [area] => Array                        (                            [12] => Array                                (                                    [click] => 8                                    [traffic] => 40                                )                            [8] => Array                                (                                    [click] => 5                                    [traffic] => 50                                )                        )                )        ))
Copy after login

感谢四楼正解

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template