我尝试在php中解析弹性搜索响应。 当我使用var_dump打印我的json结果时,我得到了:
Array (
[took] => 6
[timed_out] =>
[_shards] => Array (
[total] => 5
[successful] => 5
[failed] => 0
)
[hits] => Array (
[total] => 1
[max_score] => 0.44896343
[hits] => Array (
[0] => Array (
[_index] => car
[_type] => car
[_id] => DqE0c4ygRgC81o39DNmwhQ
[_score] => 0.44896343
[_source] => Array (
[currency] => EUR
[link] => myrurl
[reference] => A785454A
[brand] => mybrand
[model] => mymodel
[description] =>
[link_picture] => mylinkpicture
[price] => myprice
[km] => mykm
[start_years] =>
[active] => 1
[title] => mytitle
[ranking] => 22
[date_create] => 2014-05-26
)
)
)
)
)
但是,当我尝试列出或找到一个foreach的标题时:
$myData = json_decode($response);
foreach ($myobj->hits->hits as $result) {
echo $result->_source->title;
}
这不起作用,因为我得到了这个错误:
消息:为foreach()提供的参数无效
我不明白为什么。 谢谢你。
答案 0 :(得分:2)
在foreach中你试图使用“$ myobj”。 但解码的json在“$ myData”中。