0

I want to retrieve an elements of object from an array

I tried with but didn't get

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

here is my array

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

    )

I want to get

 [clothing]
 [men]
 [children]
user2046638
  • 366
  • 3
  • 17

3 Answers3

2

You don't need stdClass in your example:

To get the object:

$api_response_decode['data']

To get the sales object:

$api_response_decode['data']->sales

To get, say, clothing:

$api_response_decode['data']->sales->clothing
Jim
  • 21,521
  • 5
  • 49
  • 80
1

is $api_response_decode an object or an array? How are you getting it?

However, you could retrieve the sales items with get_object_vars.

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

prints

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

Edit: it seems $api_response_decode is an object, so I edited accordingly.

ffflabs
  • 15,728
  • 5
  • 48
  • 71
1

stdClass is a type, not a value.

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

This subject can give you more informations of what stdClass is and how to use it : What is stdClass in PHP?

Community
  • 1
  • 1
fdehanne
  • 1,759
  • 1
  • 16
  • 27