php - 如何解析 std 对象以获取特定数据
问题描述
我尝试将此结果转换为数组,我使用了 json_decode() 但我总是得到 null 然后我使用 Service_json() 并解决了问题。
在那之后,我得到了这个结果,但现在我很难得到一些特定的数据,比如类别、标称、品牌和它们的值,我得到了空结果。
这是数组:
array(1) {
["hits"]=>
object(stdClass)#3 (1) {
["hits"]=>
array(7) {
[0]=>
object(stdClass)#4 (2) {
["_id"]=>
string(20) "kN5iEXIBfVAlCluoT3sT"
["_source"]=>
object(stdClass)#5 (3) {
["categorie"]=>
string(7) "Bonbons"
["marque"]=>
string(6) "MENTOS"
["nom"]=>
string(29) "Bonbons parfums fruits MENTOS"
}
}
[1]=>
object(stdClass)#6 (2) {
["_id"]=>
string(20) "kd5iEXIBfVAlCluoT3sT"
["_source"]=>
object(stdClass)#7 (3) {
["categorie"]=>
string(7) "Bonbons"
["marque"]=>
string(6) "MENTOS"
["nom"]=>
string(21) "Bonbons menthe MENTOS"
}
}
[2]=>
object(stdClass)#8 (2) {
["_id"]=>
string(20) "kt5iEXIBfVAlCluoT3sT"
["_source"]=>
object(stdClass)#9 (3) {
["categorie"]=>
string(7) "Bonbons"
["marque"]=>
string(6) "MENTOS"
["nom"]=>
string(37) "Bonbons caramel/chocolat blanc MENTOS"
}
}
[3]=>
object(stdClass)#10 (2) {
["_id"]=>
string(20) "k95iEXIBfVAlCluoT3sT"
["_source"]=>
object(stdClass)#11 (3) {
["categorie"]=>
string(7) "Bonbons"
["marque"]=>
string(6) "Mentos"
["nom"]=>
string(31) "Bonbons caramel/chocolat MENTOS"
}
}
[4]=>
object(stdClass)#12 (2) {
["_id"]=>
string(20) "lN5iEXIBfVAlCluoT3sT"
["_source"]=>
object(stdClass)#13 (3) {
["categorie"]=>
string(7) "Bonbons"
["marque"]=>
string(6) "MENTOS"
["nom"]=>
string(28) "Bonbons menthe sucres MENTOS"
}
}
[5]=>
object(stdClass)#14 (2) {
["_id"]=>
string(20) "ld5iEXIBfVAlCluoT3sT"
["_source"]=>
object(stdClass)#15 (3) {
["categorie"]=>
string(7) "Bonbons"
["marque"]=>
string(6) "MENTOS"
["nom"]=>
string(31) "Bonbons framboise orange MENTOS"
}
}
[6]=>
object(stdClass)#16 (2) {
["_id"]=>
string(20) "lt5iEXIBfVAlCluoT3sT"
["_source"]=>
object(stdClass)#17 (3) {
["categorie"]=>
string(7) "Bonbons"
["marque"]=>
string(6) "MENTOS"
["nom"]=>
string(26) "Bonbons pomme verte MENTOS"
}
}
}
}
}
如何解析此数组以获取类别 nom 和 marque 及其值?
解决方案
未经测试,但我想这是您使用foreach
,获取内部元素值所需要的
foreach($result['hits']->hits as $key=>$value){
echo $value->_source->categories, $value->_source->marque, $value->_source->nom.PHP_EOL;
}
推荐阅读
- python - 在多线程代码中使用 sqlite 时出现分段错误
- spring-boot - 如何在 swagger UI Api 中接受对象数组的输入
- javascript - 你如何使用 Lodash/FP 中的 'flow' 来传递 'this'
- javascript - 下拉框未在 Chrome 扩展中显示数组
- angularjs - 如何在文件上传方法中添加附加参数并进入控制器端?
- android - 反应原生地图视图不加载地图
- python - 如何检测何时单击矩形对象、图像或精灵
- c++ - 为 shell 命令转义文件名
- c# - Unity ScriptableObject 参考传递给 Monobehaviour 的 ScriptableObject
- javascript - 如何使这个 JavaScript 函数也与 Internet Explorer 兼容