The same element addition problem in multidimensional arrays

Source: Internet
Author: User
First post, leave master
I took a five-dimensional array from Redis and I needed to process the addition, as follows
$a = Array (0 = = Array (0=> array ("date" = "201201", "area" = = Array (click + = "3", traffic = "+")),1=> Array ("Date" and "201202", "area" = = Array (click + = "8", traffic = "" + "),8=> ar Ray (Click + = "5", traffic = ""))), 1 = = Array ("date" = "201201", "area" = = = Array (arr. Ay (click + = "9", Traffic "+"),8=> Array (click = "6", traffic = "20120")),1=> Array ("date" and "=") 2 "," area "= NULL));


Hope that after the data processing, the same date premise, the same key in the area corresponding to the click and traffic values, namely 0 and 1 of the two large arrays, date 201001, and area 12 corresponding to the click and traffic processing should be 12 and 50, 8 corresponds to 6 and 60, and other analogy, to seek the traversal or other methods to deal with success, thank you!


Reply to discussion (solution)

What is the final result? Put it on.

The results are as follows, thanks for attention

$a = Array (0 = = Array (             0=> Array (                             "date" = "201201",                "area"                    = = Array ( C5/>click = "A",                        traffic = "6"                    ),8=> Array (                        click + = "+", traffic "+"                    ) 
  )            ),                 1=> Array (                             "date" = "201202",                "area" = = Array (                        click = > "8",                        traffic = "+"                    ),                    8=> Array (                        click + = "5",                        traffic = "                    " ")                )            )                         )              );

$t = Array (), foreach ($a as $k = = $v) {foreach ($v as $k 1=> $v 1) {$t [0][$k 1][' date '] = $v 1[' Date '];foreach ($v 1[' area '] a S $k 2=> $v 2) {$t [0][$k 1][' area '] [$k 2][' click '] + = $v 2[' click ']; $t [0][$k 1][' area '] [$k 2][' traffic '] + = $v 2[' Traffic ' ];}}} echo "
";p Rint_r ($t); echo"
";


The results are as follows:
Array ([0] = = Array ([0] = = Array ([Date] = 201201                                [Area] = = Array ([+] = array                                ([click] = [Traffic] = 50                                    ) [8] = = Array (                         [Click] = 6 [traffic] + 60) )) [1] = = Array ([Date] = 201                                202 [Area] = = Array ([+] = array ([Click] = 8 [traffic] =&gt ;                    40            ) [8] = = Array (                        [Click] = 5 [traffic] + 50) )                )        ))

Thank you for the four floor positive solution

  • 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.