将多维数组值回显为可读格式

时间:2015-08-07 12:31:48

标签: php arrays multidimensional-array

我有一个来自谷歌分析API的疯狂阵列:

print_r($visits);

产生以下内容:

Array ( [http_code] => 200 [kind] => analytics#gaData [id] => https://www.googleapis.com/analytics/v3/data/ga?ids=ga:615743&dimensions=ga:date&metrics=ga:visits&start-date=2015-07-07&end-date=2015-08-07 [query] => Array ( [start-date] => 2015-07-07 [end-date] => 2015-08-07 [ids] => ga:615743 [dimensions] => ga:date [metrics] => Array ( [0] => ga:visits ) [start-index] => 1 [max-results] => 1000 ) [itemsPerPage] => 1000 [totalResults] => 32 [selfLink] => https://www.googleapis.com/analytics/v3/data/ga?ids=ga:615743&dimensions=ga:date&metrics=ga:visits&start-date=2015-07-07&end-date=2015-08-07 [profileInfo] => Array ( [profileId] => 615743 [accountId] => 391435 [webPropertyId] => UA-391435-1 [internalWebPropertyId] => 642064 [profileName] => www.website.co.uk [tableId] => ga:615743 ) [containsSampledData] => [columnHeaders] => Array ( [0] => Array ( [name] => ga:date [columnType] => DIMENSION [dataType] => STRING ) [1] => Array ( [name] => ga:visits [columnType] => METRIC [dataType] => INTEGER ) ) [totalsForAllResults] => Array ( [ga:visits] => 8250 ) [rows] => Array ( [0] => Array ( [0] => 20150707 [1] => 271 ) [1] => Array ( [0] => 20150708 [1] => 266 ) [2] => Array ( [0] => 20150709 [1] => 251 ) [3] => Array ( [0] => 20150710 [1] => 264 ) [4] => Array ( [0] => 20150711 [1] => 351 ) [5] => Array ( [0] => 20150712 [1] => 244 ) [6] => Array ( [0] => 20150713 [1] => 309 ) [7] => Array ( [0] => 20150714 [1] => 250 ) [8] => Array ( [0] => 20150715 [1] => 277 ) [9] => Array ( [0] => 20150716 [1] => 214 ) [10] => Array ( [0] => 20150717 [1] => 215 ) [11] => Array ( [0] => 20150718 [1] => 167 ) [12] => Array ( [0] => 20150719 [1] => 228 ) [13] => Array ( [0] => 20150720 [1] => 290 ) [14] => Array ( [0] => 20150721 [1] => 236 ) [15] => Array ( [0] => 20150722 [1] => 245 ) [16] => Array ( [0] => 20150723 [1] => 267 ) [17] => Array ( [0] => 20150724 [1] => 307 ) [18] => Array ( [0] => 20150725 [1] => 271 ) [19] => Array ( [0] => 20150726 [1] => 226 ) [20] => Array ( [0] => 20150727 [1] => 319 ) [21] => Array ( [0] => 20150728 [1] => 299 ) [22] => Array ( [0] => 20150729 [1] => 263 ) [23] => Array ( [0] => 20150730 [1] => 242 ) [24] => Array ( [0] => 20150731 [1] => 233 ) [25] => Array ( [0] => 20150801 [1] => 165 ) [26] => Array ( [0] => 20150802 [1] => 170 ) [27] => Array ( [0] => 20150803 [1] => 349 ) [28] => Array ( [0] => 20150804 [1] => 410 ) [29] => Array ( [0] => 20150805 [1] => 282 ) [30] => Array ( [0] => 20150806 [1] => 256 ) [31] => Array ( [0] => 20150807 [1] => 113 ) ) ) 

如果我用

替换print_r($visits);
foreach ($visits as $key => $val) {
   echo $val;
}

我得到以下更易读的内容:

200analytics#gaDatahttps://www.googleapis.com/analytics/v3/data/ga?ids=ga:615743&dimensions=ga:date&metrics=ga:visits&start-date=2015-07-07&end-date=2015-08-07Array100032https://www.googleapis.com/analytics/v3/data/ga?ids=ga:615743&dimensions=ga:date&metrics=ga:visits&start-date=2015-07-07&end-date=2015-08-07ArrayArrayArrayArray

我的问题是,如何访问此阵列中的数组?

理想情况下,我希望以可读的方式打印整个$visits数组。

2 个答案:

答案 0 :(得分:1)

您可以使用array_walk_recursive()循环遍历数组的每个值,例如

array_walk_recursive($visits, function($v, $k){
    echo $v . "<br>";
});

答案 1 :(得分:1)

如果你想要这个用于调试,那么输出如下:

echo '<pre>';
print_r($visits);
echo '</pre>';

其他方式使用array_walk_recursive()