2012-04-12 10 views
0

私は複数の配列を持っています2、最初の配列は都市(ムンバイ、プネー、バンガロール、ハイデラバード) 2番目の配列はMumbia nad Hyderabdです。PHPのキーを基にした多次元配列の合計

私はキーに基づいて値を合計したいと思います。

アレイ ( [ムンバイ] =>配列 ( [GA:ページビュー] => 1 [GA:訪問] => 1 [GA:newVisits] => 1 [GA:pageviewsPerVisit] = > 1 [GA:entranceBounceRate] => 1 [GA:avgTimeOnSite] => 1 [GA:timeOnSite] => 1 [GA:バウンス] => 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 
    ) 

2配列:

アレイ ( [ムンバイ] =>配列 ( [GA:ページビュー] => 5 [GA:訪問] => 5 [GA:newVisits] => 5 [GA:pageviewsPerVisit] => 5 [GA:entranceBounceRate] => 5 [GA:avgTimeOnSite] => 5 [GA:timeOnSite] => 5 [GA:バウンス] => 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 
    ) 

Iのように結果が欲しい:

アレイ ( [ムンバイ] =>配列 ( [GA:ページビュー] => 6 [GA:訪問] => 6 [GA :newVisits] => 6 [GA:pageviewsPerVisit] => 6 [GA:entranceBounceRate] => 6 [GA:avgTimeOnSite] => 6 [GA:timeOnSite] => 6 [GA:バウンス] => 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 
    ) 

)あなたの助けを

おかげで.... ルース。

あなたのためにそれを行う必要があります

答えて

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

:)

+1

という名前の配列にこのすべてを言うことができます試してみてください値を連結するのではなく、合計するには '+ ='を使います。 – Alasjo

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 
+0

ありがとうございます..それは働いた... :) – user1328178

+0

あなたは大歓迎です。 :) –

0

は、私たちは、私はあなたが意味確信している親

$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; 
      } 
    } 
}