[我试图将结果转换为数组,我使用json_decode(),但我总是为null,然后使用Service_json(),我解决了这个问题。
[此后,我得到了这个结果,但是现在我很难获取一些特定的数据,例如categorie,nom,marque及其值,而我得到的结果是空的。
这里是数组:
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;
}
请参阅下面的示例以了解如何处理您的情况。