如何从数组中检索对象


how to retrieve object from array?

我想从数组中检索对象的元素

我试过了,但是没有得到

  $api_response_decode['data']->stdClass->sales->stdClass

这里是数组

 [data] => stdClass Object
    (
        [sales] => stdClass Object
            (
                [clothing] => 2
                [men] => 0
                [children] => 4
            )
    )

我想得到

 [clothing]
 [men]
 [children]

您的示例中不需要stdClass:

获取对象:

$api_response_decode['data']

获取销售对象:

$api_response_decode['data']->sales

获取,例如,服装:

$api_response_decode['data']->sales->clothing

stdClass是一个类型,而不是一个值。

$api_response_decode['data']->sales->clothing

这个主题可以给你更多的信息,什么是stdClass和如何使用它:什么是stdClass在PHP?

$api_response_decode是对象还是数组?你是怎么得到它的?

但是,您可以使用get_object_vars检索销售项目。

$items=get_object_vars($api_response_decode->data->sales);
var_dump($items);

打印

array(3) {
  ["clothing"]=>   int(2)
  ["men"]=>    int(0)
  ["children"]=>  int(4)
}

编辑:看起来$api_response_decode是一个对象,所以我做了相应的编辑