Php 如何解析std对象以获取特定数据
我试图将这个结果转换为数组,我使用了json_decode(),但我总是得到null,然后我使用了Service_json(),我解决了这个问题 在那之后,我得到了这个结果,但是现在我很难得到一些特定的数据,比如分类、nom、marque和它们的值,结果是空的 以下是阵列:Php 如何解析std对象以获取特定数据,php,arrays,loops,object,associative-array,Php,Arrays,Loops,Object,Associative Array,我试图将这个结果转换为数组,我使用了json_decode(),但我总是得到null,然后我使用了Service_json(),我解决了这个问题 在那之后,我得到了这个结果,但是现在我很难得到一些特定的数据,比如分类、nom、marque和它们的值,结果是空的 以下是阵列: array(1) { ["hits"]=> object(stdClass)#3 (1) { ["hits"]=> array(7) { [0]=> obje
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"
}
}
}
}
}
如何解析此数组以获取分类名称和标记及其值?未测试,但我猜这就是使用
foreach获取内部元素值所需的内容
foreach($result['hits']->hits as $key=>$value){
echo $value->_source->categories, $value->_source->marque, $value->_source->nom.PHP_EOL;
}
有关如何处理您的情况,请参见下面的示例
class Person implements JsonSerializable
{
protected $id;
protected $name;
public function __construct(array $data)
{
$this->id = $data['id'];
$this->name = $data['name'];
}
public function getId()
{
return $this->id;
}
public function getName()
{
return $this->name;
}
public function jsonSerialize()
{
return
[
'id' => $this->getId(),
'name' => $this->getName()
];
}
}
$person = new Person(array('id' => 1, 'name' => 'Amir'));
echo json_encode($person,JSON_FORCE_OBJECT);
要获取json结果(字符串),以便能够遍历,请执行以下操作:
$json = json_decode($json);
foreach($json as $obj){
echo $obj->name;
.....
}
循环遍历对象并使用以下内容获取其成员:
$json = json_decode($json);
foreach($json as $obj){
echo $obj->name;
.....
}
我希望这个工作流程能帮助你实现你的愿望。我猜你是在使用Elasticsearch来获得点击,所以你的点击是以对象数组的形式出现的。
,Simpleforeach
会在点击中进行解析以获得点击谢谢你的回答很有帮助@Always@Ana很高兴它对你有所帮助,祝你好运:)