如果子键存在,则Sum Multi Dimensional数组

时间:2014-02-03 11:50:51

标签: php arrays

我有这个数组有100多个结果,但其中一些有相同的子数组键。我想在这个例子中对数组元素进行求和,该元素具有相同的键[/xyx/888350]。但是,我想保持格式,因为它是二维的。

 Array
    (
        [0] => Array
            (
                [/xyx/888350] => /xyx/888350
                [visitors] => 1
                [pageviews] => 2
                [uniquepageviews] => 1
            )

        [1] => Array
            (
                [/xyx/888350] => /xyx/888350
                [visitors] => 1
                [pageviews] => 3
                [uniquepageviews] => 1
            )

        [2] => Array
            (
                [/xyx/888350] => /xyx/888350
                [visitors] => 1
                [pageviews] => 2
                [uniquepageviews] => 1
            )
       [3] => Array
            (
                [/xyx/102254] => /xyx/102254
                [visitors] => 1
                [pageviews] => 2
                [uniquepageviews] => 1
            )
    )

我期待以下内容:

Array
(
    [0] => Array
        (
            [/xyx/888350] => /xyx/888350
            [visitors] => 2
            [pageviews] => 7
            [uniquepageviews] => 2
        )

    [1] => Array
        (
            [/xyx/102254] => /xyx/102254
            [visitors] => 1
            [pageviews] => 3
            [uniquepageviews] => 1
        )
)

提前致谢。

2 个答案:

答案 0 :(得分:2)

循环数组,并将结果存储在临时数组中,方法是将第一个值用作键:

  $input = /*your example data here*/;
  $result = array();

  foreach($input as $data){
     $keys = array_keys($data);
     $key = $keys[0]; //get the first key of the array a.k.a '/xyx/888350'
     if(isset($result[$key])){
          //sum the values if we have this key
          $result[$key]['visitors']         += $data['visitors'];
          $result[$key]['pageviews']        += $data['pageviews'];
          $result[$key]['uniquepageviews']  += $data['uniquepageviews'];
     }else{
          $result[$key] = $data;
     }
  }

  //drop the extra keys and return a indexed array with the summed values
  return array_values($result);

答案 1 :(得分:0)

$results=$service->data_ga->get("");
$stats = array();
for ($i=0; $i < count($stats_results=$results->getRows()) ; $i++)
{
   $stats[]=array(
     'path'=>$stats_results[$i][1],
     'visitors'=>$stats[$i]['visitors'] + $stats_results[$i][2],
     'pageviews'=>$stats[$i]['visitors'] + $stats_results[$i][3],
     'uniquepageviews'=>$stats[$i]['visitors'] + $stats_results[$i][4]
   );
}
$result = array();
foreach($stats as $data){
     $keys = array_keys($data);
     $key = $keys[0]; //get the first key of the array a.k.a '/xyx/888350'
     if(isset($result[$key]))
     {
       //sum the values if we have this key
       $result[$key]['visitors']         += $data['visitors'];
       $result[$key]['pageviews']        += $data['pageviews'];
       $result[$key]['uniquepageviews']  += $data['uniquepageviews'];

    }else{
       $result[$key] = $data;
    }
}

echo "<pre>";
print_r($result);

输出:

Array
(
    [path] => Array
        (
            [path] => /path/888350
            [visitors] => 3
            [pageviews] => 7
            [uniquepageviews] => 3
        )

)

谢谢你们两位。随着时间的推移,我也会回答其他人的问题。我刚开始我的职业生涯。谢谢你们:)