如何在PHP中访问stdClass对象的元素
作者:互联网
我已经尝试了其他帖子的解释我无法让它工作,因为我总是得到以下警告:警告:get_object_vars()期望参数1是对象,数组给出…
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
)
)
我尝试分别访问值 – modas和pares及其数字 – 例如,第一个元素是“pares”,它的值是“4”,例如使用
echo get_object_vars($modas_pares)['0']['pares'];
但我得到以下警告:
Warning: get_object_vars() expects parameter 1 to be object, array
given in
那我怎样才能访问这些值?
解决方法:
$objectVar = $modas_pares[0]->pares; //spits out 4
标签:stdclass,php,arrays,object 来源: https://codeday.me/bug/20190825/1714292.html