我有JSON数据:
{"took":3,
"timed_out":false,
"_shards":{ "total":5,
"successful":5,
"failed":0
},
"hits":{"total":105,
"max_score":1.0,
"hits":[
{"_index":"csv",
"_type":"logs",
"_id":"AVeyr-BQodQ9UhW1sdW0",
"_score":1.0,
"_source":{"message":"james,wonder\r",
"@version":"1",
"@timestamp":"2016-10-11T07:40:52.332Z",
"path":"D:\\logstash-2.4.0\\bin\\Book1.csv",
"host":"CHNL-LT-544",
"fname":"fa1368a93aa39b2346329c1734be1f4b",
"lname":"wonder"
}
},
{"_index":"csv",
"_type":"logs",
"_id":"AVeyr-BQodQ9UhW1sdW1",
"_score":1.0,
"_source":{"message":"muskaan, wonder\r",
"@version":"1",
"@timestamp":"2016-10-11T07:40:52.332Z",
"path":"D:\\logstash-2.4.0\\bin\\Book1.csv",
"host":"CHNL-LT-544",
"fname":"479085e94d305129527fa80978613e95",
"lname":"wonder"}
},
{"_index":"csv","_type":"logs","_id":"AVeyr-BQodQ9UhW1sdW3","_score":1.0,"_source":{"message":"altimetrik,india\r","@version":"1","@timestamp":"2016-10-11T07:40:52.333Z","path":"D:\\logstash-2.4.0\\bin\\Book1.csv","host":"CHNL-LT-544","fname":"8b3ecf275832c79b36d62d74f47257c9","lname":"india"}},{"_index":"csv","_type":"logs","_id":"AVeyr-BQodQ9UhW1sdW8","_score":1.0,"_source":{"message":"kool,indiae\r","@version":"1","@timestamp":"2016-10-11T07:40:52.335Z","path":"D:\\logstash-2.4.0\\bin\\Book1.csv","host":"CHNL-LT-544","fname":"d2edbdce885720c70f38f36748c37600","lname":"indiae"}},{"_index":"csv","_type":"logs","_id":"AVeyr-BQodQ9UhW1sdXA","_score":1.0,"_source":{"message":"ds,dssd\r","@version":"1","@timestamp":"2016-10-11T07:40:52.336Z","path":"D:\\logstash-2.4.0\\bin\\Book1.csv","host":"CHNL-LT-544","fname":"91594a75bc6bfb0ec800d4f454e1fe87","lname":"dssd"}},{"_index":"csv","_type":"logs","_id":"AVeytJ0iodQ9UhW1sdXf","_score":1.0,"_source":{"message":"kool,india\r","@version":"1","@timestamp":"2016-10-11T07:46:02.839Z","path":"D:\\logstash-2.4.0\\bin\\Book1.csv","host":"CHNL-LT-544","fname":"d2edbdce885720c70f38f36748c37600","lname":"india"}},{"_index":"csv","_type":"logs","_id":"AVeytJ0iodQ9UhW1sdXi","_score":1.0,"_source":{"message":"karnataka,india\r","@version":"1","@timestamp":"2016-10-11T07:46:02.840Z","path":"D:\\logstash-2.4.0\\bin\\Book1.csv","host":"CHNL-LT-544","fname":"2a0785caed6bfd2ffb6ad5a449f2bd0e","lname":"india"}},{"_index":"csv","_type":"logs","_id":"AVeytv9hodQ9UhW1sdXq","_score":1.0,"_source":{"message":"james,wonder\r","@version":"1","@timestamp":"2016-10-11T07:48:39.550Z","path":"D:\\logstash-2.4.0\\bin\\Book1.csv","host":"CHNL-LT-544","fname":"fa1368a93aa39b2346329c1734be1f4b","lname":"wonder"}},{"_index":"csv","_type":"logs","_id":"AVeytv9hodQ9UhW1sdX0","_score":1.0,"_source":{"message":"rajasthan,india\r","@version":"1","@timestamp":"2016-10-11T07:48:39.558Z","path":"D:\\logstash-2.4.0\\bin\\Book1.csv","host":"CHNL-LT-544","fname":"449b07d9038569b2d69243dadfae0371","lname":"india"}},{"_index":"csv","_type":"logs","_id":"AVeytv9hodQ9UhW1sdX5","_score":1.0,"_source":{"message":"najeeb,shariff\r","@version":"1","@timestamp":"2016-10-11T07:48:39.573Z","path":"D:\\logstash-2.4.0\\bin\\Book1.csv","host":"CHNL-LT-544","fname":"be7a5e98258338fa63086f44e8bd1850","lname":"shariff"}}]}}
我想从这个JSON中只打印2个字段; fname
和lname
。请告诉我如何使用PHP实现这一目标。
$url = 'localhost:9200/csv/_search';;
$content = file_get_contents($url);
$json = json_decode($content, true);
foreach($json['hits'] as $item) {
print $item['fname'];
}
但是获得错误未定义索引
答案 0 :(得分:1)
$url = 'localhost:9200/csv/_search';;
$content = file_get_contents($url);
$json = json_decode($content, true);
foreach($json['hits']['hits'] as $item) {
echo "<p>{$item['_source']['fname']}</p>";
echo "<p>{$item['_source']['lname']}</p>";
}
fname
和lname
的数组路径类似于[hits][hits][0][_source][fname]
和[hits][hits][0][_source][lname]
。应在循环中迭代[hits][hits]
以访问每个_source
元素。
正如 RiggsFolly 所说,您需要print_r($json)
来查看阵列的结构,尤其是在遇到嵌套数组时。
希望它有所帮助!