将维度和指标数据添加到数组

时间:2017-05-26 08:25:44

标签: php arrays multidimensional-array google-analytics

使用以下代码(来自Analytics quickstart),我尝试将维度和指标数据添加到$responseArray数组,以便稍后将其作为JSON返回:

public function formatDailySessionsReport( $reports ) {
    $responseArray = array();
    for ( $reportIndex = 0; $reportIndex < count( $reports ); $reportIndex++ ) {
        $report = $reports[ $reportIndex ];
        $header = $report->getColumnHeader();
        $dimensionHeaders = $header->getDimensions();
        $metricHeaders = $header->getMetricHeader()->getMetricHeaderEntries();
        $rows = $report->getData()->getRows();

        for ( $rowIndex = 0; $rowIndex < count($rows); $rowIndex++) {
            $row = $rows[ $rowIndex ];
            $dimensions = $row->getDimensions();
            $metrics = $row->getMetrics();
            for ($i = 0; $i < count($dimensionHeaders) && $i < count($dimensions); $i++) {
                //Adding dimension data here
                $responseArray[$rowIndex][$i][$dimensionHeaders[$i]] = $dimensions[$i];
                //print($dimensionHeaders[$i] . ": " . $dimensions[$i] . "\n");
            }

            for ($j = 0; $j < count($metrics); $j++) {
                $values = $metrics[$j]->getValues();
                for ($k = 0; $k < count($values); $k++) {
                    $entry = $metricHeaders[$k];
                    //Adding metric data here
                    $responseArray[$j][$k][$entry->getName()] = $values[$k];
                    //print($entry->getName() . ": " . $values[$k] . "\n");
                }
            }
        }
    } 
    return $responseArray;      
}

使用注释掉的print语句时,数据似乎打印正确:

ga:date: 20170426
sessions: 2711
ga:date: 20170427
sessions: 2425
ga:date: 20170428
sessions: 2620

但是,当尝试将其添加到数组时,数据似乎没有正确格式化(会话只包含一次):

"response": [
    [
      {
        "ga:date": "20170426",
        "sessions": "656"
      }
    ],
    [
      {
        "ga:date": "20170427"
      }
    ],
    [
      {
        "ga:date": "20170428"
      }
    ],
    ...
]

我使用以下命令创建JSON响应:

$response = $this->formatDailySessionsReport( $reportData );
// Return response
echo json_encode( array( 'response' => $response ) );

我期待更像这样的事情:

"response" [
  {
    "ga:date": "20170426",
    "sessions": "656"
  },
  {
    "ga:date": "20170427",
    "sessions": "1234"
  },
  {
    "ga:date": "20170428",
    "sessions": "1234"
  }  
]

将日期和会话放在一起,如何构建$responseArray以获得我预期的格式?

2 个答案:

答案 0 :(得分:0)

您可以将此代码用于array_map(),

$responseArray['response'] = array_map(function($v){
  return $v[0];
}, $responseArray['response']);

答案 1 :(得分:0)

事实证明,我在for循环中使用了错误的索引:

for ( $rowIndex = 0; $rowIndex < count($rows); $rowIndex++) {
    $row = $rows[ $rowIndex ];
    $dimensions = $row->getDimensions();
    $metrics = $row->getMetrics();
    for ($i = 0; $i < count($dimensionHeaders) && $i < count($dimensions); $i++) {
        $responseArray[$rowIndex][$i][$dimensionHeaders[$i]] = $dimensions[$i];
        //print($dimensionHeaders[$i] . ": " . $dimensions[$i] . "\n");
    }

    for ($j = 0; $j < count($metrics); $j++) {
        $values = $metrics[$j]->getValues();
        for ($k = 0; $k < count($values); $k++) {
            $entry = $metricHeaders[$k];
            $responseArray[$rowIndex][$k][$entry->getName()] = $values[$k];
            // wrong index ^^^^^^^^^ <-- Here
            //print($entry->getName() . ": " . $values[$k] . "\n");
        }
    }
}