Question

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?

Was it helpful?

Solution

$objectVar = $modas_pares[0]->pares; //spits out 4

OTHER TIPS

echo $modas_pares[0]->pares;

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

Array items are accessed with brackets []

Object properties with arrow ->

0 is a number, so don't use quotes

echo $modas_pares[0]->pares;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top