3

i have tried the other posts' explanations i can't get it to work since i always get the following warning: Warning: get_object_vars() expects parameter 1 to be object, array given in ...

The stdclass object array looks like this:

Array
(
    [0] => stdClass Object
        (
            [pares] => 4
            [moda] => 9
        )

    [1] => stdClass Object
        (
            [pares] => 3
            [moda] => 8
        )

    [2] => stdClass Object
        (
            [pares] => 2
            [moda] => 8
        )

    [3] => stdClass Object
        (
            [pares] => 5
            [moda] => 4
        )

    [4] => stdClass Object
        (
            [pares] => 1
            [moda] => 1
        )

    [5] => stdClass Object
        (
            [pares] => 6
            [moda] => 1
        )

)

And i try to access the values - modas and pares and their numbers respectively - for example, the very first element which is "pares" and its value "4" by using, for example

echo get_object_vars($modas_pares)['0']['pares'];

but i get the following warning:

Warning: get_object_vars() expects parameter 1 to be object, array given in

Then how can i access these values?

1
  • $modas_pares[0]->pares Commented Aug 1, 2014 at 12:31

3 Answers 3

8
$objectVar = $modas_pares[0]->pares; //spits out 4
Sign up to request clarification or add additional context in comments.

Comments

0
echo $modas_pares[0]->pares;

Should give you access to the value for the first object's property pares.

1 Comment

I had been writing so much Javascript lately that I forgot that you access object properties using -> in PHP. That's why I got a downvote.
0

Array items are accessed with brackets []

Object properties with arrow ->

0 is a number, so don't use quotes

echo $modas_pares[0]->pares;

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.