gpt4 book ai didi

php - 如何解析PHP中的elasticsearch响应

转载 作者:行者123 更新时间:2023-12-03 01:07:59 25 4
gpt4 key购买 nike

我尝试在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;
}

那行不通,因为我收到此错误:

Message: Invalid argument supplied for foreach()



我不明白为什么。
提前感谢。

最佳答案

在foreach中,您尝试使用“$ myobj”。
但是解码后的json位于“$ myData”中。

关于php - 如何解析PHP中的elasticsearch响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23887629/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com