如何在php中添加数组中的值?

时间:2014-04-25 06:47:33

标签: php arrays

我有两个变量:$report1$report2

 function overallStats($startdate, $enddate) {
          $reports1 = $analytic_report->reports->query('contentOwner==first',
          $startdate, $enddate, 'views,earnings', array(
          'dimensions' => 'day',
          'filters' => 'claimedStatus==claimed',
          'sort' => 'day'
          ));
          $reports2 = $analytic_report->reports->query('contentOwner==second',
          $startdate, $enddate, 'views,earnings', array(
          'dimensions' => 'day',
          'filters' => 'claimedStatus==claimed',
          'sort' => 'day'
          ));
          $reports =  array_merge($reports1, $reports2);
    }

    function second(){
        $overallrawstats = $this->overallStats(date('Y-m-d', strtotime('9 days ago')), date('Y-m-d', strtotime('yesterday')));
        if (!empty($overallrawstats)) {
            $overallstats = array();
            foreach($overallrawstats as $p=>$o) {
              $overallstats[] = array(
              recorddate => $o[0],
              views => $o[1],
              revenue => $o[2]
              );
            }
        }
    }

我想得到第三个数组,它合并了这两个数组的所有值,但当我在其中获得第三个合并数组时,视图和收入应该添加这两个数组。我怎么才能得到它?

1 个答案:

答案 0 :(得分:0)

假设两个数组的长度相同:

$report3 = array();
foreach ($report1 as $index => $value) {
    array_push($report3, (int) $value + (int) $report2[$index]);
}