【问题标题】:How to parse an std object to get specific data如何解析 std 对象以获取特定数据
【发布时间】:2020-09-12 21:05:02
【问题描述】:

我试图将此结果转换为数组,我使用了 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 及其值?

【问题讨论】:

  • 我猜你正在使用 Elasticsearch 来获取命中,所以你有格式为 array of object 的命中,简单的 foreach 将进行解析以获取命中内的命中

标签: php arrays loops object associative-array


【解决方案1】:

未测试,但我想这是您使用 foreach 获取内部元素值所需要的,

foreach($result['hits']->hits as $key=>$value){
   echo $value->_source->categories, $value->_source->marque, $value->_source->nom.PHP_EOL;
}

【讨论】:

  • 非常感谢您的回答很有帮助@Always
  • @Ana 很高兴它能以某种方式帮助你,祝你好运:)
【解决方案2】:

请参阅下面的示例了解如何处理您的情况。

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;
   .....

}

我希望这个工作流程可以帮助您实现您的愿望。

【讨论】:

    猜你喜欢
    • 2021-08-17
    • 2012-01-13
    • 1970-01-01
    • 1970-01-01
    • 2018-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-08
    相关资源
    最近更新 更多