I've parsed basic JSON files with no trouble before, but the structure of this one (from ElasticSearch) is completely confusing me. Here's a stripped down example of the JSON I'm working with:
{
"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"
}
}
]
}
}
Now, let's say I want to get the 'title' value of both results result. I have tried a lot of different things with no success. For example:
//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;
}
I've tried a lot of different variations, but I'm just not exactly sure how to parse this structure. Any help would be hugely appreciated.