简体   繁体   English

如何在PHP中访问stdClass对象的元素

[英]How to access elements of stdClass Object in PHP

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 ... 我已经尝试了其他帖子的解释我无法让它工作,因为我总是得到以下警告:警告:get_object_vars()期望参数1是对象,数组给出...

The stdclass object array looks like this: stdclass对象数组如下所示:

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 我尝试分别访问值 - modas和pares及其数字 - 例如,第一个元素是“pares”,它的值是“4”,例如使用

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 警告:get_object_vars()期望参数1是对象,给定的数组

Then how can i access these values? 那我怎样才能访问这些值?

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

应该给你访问该值的第一个对象的属性pares

Array items are accessed with brackets [] 使用方括号[]访问数组项

Object properties with arrow -> 带箭头的对象属性 - >

0 is a number, so don't use quotes 0是一个数字,所以不要使用引号

echo $modas_pares[0]->pares;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM