我有一个查询,在进行JSON编码时,print_r()
看起来像这样:
Array
(
[response] => Array
(
[numFound] => 2392
[start] => 0
[maxScore] => 1
[docs] => Array
(
)
)
[stats] => Array
(
[stats_fields] => Array
(
[q_one] => Array
(
[min] => 0.0059
[max] => 6.0095
[count] => 2392
[missing] => 0
[sum] => 53.9131
[sumOfSquares] => 48.16448107
[mean] => 0.022538921404682
[stddev] => 0.14012800795752
[facets] => Array
(
)
)
[q_two] => Array
(
[min] => 0
[max] => 0.0075
[count] => 2392
[missing] => 0
[sum] => 17.67
[sumOfSquares] => 0.132525
[mean] => 0.0073871237458194
[stddev] => 0.00091333432809989
[facets] => Array
(
)
)
[q_three] => Array
(
[min] => 0
[max] => 0.065
[count] => 2392
[missing] => 0
[sum] => 153.14
[sumOfSquares] => 9.9541
[mean] => 0.064021739130435
[stddev] => 0.0079155641768643
[facets] => Array
(
)
)
)
)
)
在stats >> stats_fields
内我想写一个foreach
循环来捕获每个标题的名称:q_one
,q_two
和q_three
,但是我我不知道该怎么做。
我可以捕获这3个'标题'中的每个元素,因为它们的名称是静态的(例如 - 每个元素都有min
和max
元素)
foreach ($json['stats']['stats_fields'] as $j) {
echo $j['min']
}
但我不确定如何获得标题名称......
答案 0 :(得分:5)
使用array_keys()
:
$headers = array_keys($json['stats']['stats_fields']);
如果您需要使用$key => $value
语法,请将它们放入循环中:
foreach ($json['stats']['stats_fields'] as $key => $j) {
echo $key;
echo $j['min'];
}