0

我有多个数组,现在说 2,第一个数组有城市(孟买,浦那。班加罗尔,海得拉巴),第二个数组有 Mumbia nad Hyderabd。

我想根据 Key 总结值。

数组 ( [孟买] => 数组 ( [ga:pageviews] => 1 [ga:visits] => 1 [ga:newVisits] => 1 [ga:pageviewsPerVisit] => 1 [ga:entranceBounceRate] => 1 [ ga:avgTimeOnSite] => 1 [ga:timeOnSite] => 1 [ga:bounces] => 1)

[Pune] => Array
    (
        [ga:pageviews] => 2
        [ga:visits] => 2
        [ga:newVisits] => 2
        [ga:pageviewsPerVisit] => 2
        [ga:entranceBounceRate] => 2
        [ga:avgTimeOnSite] => 2
        [ga:timeOnSite] => 2
        [ga:bounces] => 2
    )

[Bangalore] => Array
    (
        [ga:pageviews] => 3
        [ga:visits] => 3
        [ga:newVisits] => 3
        [ga:pageviewsPerVisit] => 3
        [ga:entranceBounceRate] => 3
        [ga:avgTimeOnSite] => 3
        [ga:timeOnSite] => 3
        [ga:bounces] => 3
    )

[Hyderabad] => Array
    (
        [ga:pageviews] => 4
        [ga:visits] => 4
        [ga:newVisits] => 4
        [ga:pageviewsPerVisit] => 4
        [ga:entranceBounceRate] => 4
        [ga:avgTimeOnSite] => 4
        [ga:timeOnSite] => 4
        [ga:bounces] => 4
    )

)

第二个数组:

数组 ( [孟买] => 数组 ( [ga:pageviews] => 5 [ga:visits] => 5 [ga:newVisits] => 5 [ga:pageviewsPerVisit] => 5 [ga:entranceBounceRate] => 5 [ ga:avgTimeOnSite] => 5 [ga:timeOnSite] => 5 [ga:bounces] => 5)

[Hyderabad] => Array
    (
        [ga:pageviews] => 2
        [ga:visits] => 2
        [ga:newVisits] => 2
        [ga:pageviewsPerVisit] => 2
        [ga:entranceBounceRate] => 2
        [ga:avgTimeOnSite] => 2
        [ga:timeOnSite] => 2
        [ga:bounces] => 2
    )

)

我希望结果为:

数组 ( [孟买] => 数组 ( [ga:pageviews] => 6 [ga:visits] => 6 [ga:newVisits] => 6 [ga:pageviewsPerVisit] => 6 [ga:entranceBounceRate] => 6 [ ga:avgTimeOnSite] => 6 [ga:timeOnSite] => 6 [ga:bounces] => 6)

[Pune] => Array
    (
        [ga:pageviews] => 2
        [ga:visits] => 2
        [ga:newVisits] => 2
        [ga:pageviewsPerVisit] => 2
        [ga:entranceBounceRate] => 2
        [ga:avgTimeOnSite] => 2
        [ga:timeOnSite] => 2
        [ga:bounces] => 2
    )

[Bangalore] => Array
    (
        [ga:pageviews] => 3
        [ga:visits] => 3
        [ga:newVisits] => 3
        [ga:pageviewsPerVisit] => 3
        [ga:entranceBounceRate] => 3
        [ga:avgTimeOnSite] => 3
        [ga:timeOnSite] => 3
        [ga:bounces] => 3
    )

[Hyderabad] => Array
    (
        [ga:pageviews] => 6
        [ga:visits] => 6
        [ga:newVisits] => 6
        [ga:pageviewsPerVisit] => 6
        [ga:entranceBounceRate] => 6
        [ga:avgTimeOnSite] => 6
        [ga:timeOnSite] => 6
        [ga:bounces] => 6
    )

)

谢谢你的帮助……露丝。

4

3 回答 3

0
foreach($arr2["Hyderabad"] as $key => $val)
{
    $arr1["Hyderabad"][$key] += $val;
}

那应该为你做:)

于 2012-04-12T05:22:23.383 回答
0

试试这个

$newarray = $array1;
foreach ($array2 as $key => $values) {
    foreach ($values as $name => $value) {
        if (!is_array($newarray[$key])) { $newarray[$key] = array(); }
        $newarray[$key][$name] += $value;
    }
}

// $newarray should now be the array you want with values added
于 2012-04-12T05:25:13.630 回答
0

让我们在一个名为 parent 的数组中说出所有这些

$parent has keys as 0,1


foreach($parent as $k=>$v)    
{
     foreach($v as $ik=>$iv)
     {
          foreach($iv as $fk=>$fv)
          {
              $cities[$ik][$fk] = $cities[$ik[$fk]+$fv;
          }
     }
 }
于 2012-04-12T05:25:18.560 回答