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]
1
  • stdClass is the object type, not the attribute name.... Commented Jun 13, 2014 at 12:42

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

2 Comments

still get annot use object of type stdClass as array :(
@user2046638 If $api_response_decode is an object also you will have to treat it like one: $api_response_decode->data
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?

2 Comments

still get cannot use object of type stdClass as array :(
Give a var_dump() of $api_response_decode. Maybe data is also an object and not an array.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.