【发布时间】:2014-07-16 06:15:16
【问题描述】:
我尝试在 php 中解析我的 elasticsearch 响应。 当我用 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() 提供的参数无效
我不明白为什么。 提前感谢。
【问题讨论】:
-
首先你叫它
$myData,后来你尝试使用$myobj。这可能是你的问题吗?
标签: php arrays json elasticsearch