【发布时间】:2014-05-31 19:19:29
【问题描述】:
我之前已经毫无问题地解析了基本的 JSON 文件,但是这个(来自 ElasticSearch)的结构完全让我感到困惑。这是我正在使用的 JSON 的精简示例:
{
"took": 7,
"timed_out": false,
"_shards": {
"total": 2,
"successful": 2,
"failed": 0
},
"hits": {
"total": 1017,
"max_score": 2.8167849,
"hits": [
{
"_index": "myindex",
"_type": "mytype",
"_id": "119479",
"_score": 2.8167849,
"_source": {
"title": "my title",
"url": "my url",
"company": "my company",
"location": "my location",
"description": "my description",
"industry": "my industry"
}
},
{
"_index": "myindex",
"_type": "mytype",
"_id": "119480",
"_score": 2.8167849,
"_source": {
"title": "my title",
"url": "my url",
"company": "my company",
"location": "my location",
"description": "my description",
"industry": "my industry"
}
}
]
}
}
现在,假设我想获取两个结果结果的“标题”值。我尝试了很多不同的事情,但都没有成功。例如:
//json_decode works fine. I have verified with a var_dump();
$myobj = json_decode($json);
//this is where I'm not sure what to do:
foreach($myobj->hits->hits->_source as $result) {
echo $result->title;
}
我尝试了很多不同的变体,但我只是不确定如何解析这个结构。任何帮助将不胜感激。
【问题讨论】:
-
var_dump($myobj)会告诉你确切的结构。 -
谢谢你,这让我走上了正轨。
标签: php json elasticsearch