繁体   English   中英

如何检索位于另一个数组内的stdClass对象数组

[英]how to retrieve a stdClass Object Array thats located inside another array

我有一个位于数组$ array1内的数组Address

[Address] => stdClass Object
        (
            [Address1] => xxxxxx
            [City] => xxxxx
            [State] => xxxxx
            [Zip] => xxxxxxx
        )

我试图调用print_r($ array1 ['Address']),但是没有用。 stdClass对象是否要求我以不同的方式调用它?

这是我的设置

我在名为CALL_FUNCTION的函数中设置了$array1 在函数内部,我使用return $array1来访问函数外部的数组。

在函数之外,我有这个...

$array1 = array();
$array1 = CALL_FUNCTION();

如果我执行print_r($array1)则返回其中具有上述std对象数组的数组。

$ array1不是数组,它还是错误所指示的对象:

致命错误:未捕获错误:无法将stdClass类型的对象用作数组

使用$array1 = array()不会执行任何操作,因为您没有使用$array1 ,而是立即将其重新分配给CALL_FUNCTION()的返回值。

作为对象访问它: $array1->Address->Address1

但是,您将需要将$array1的名称更改为$userObject类的名称,以便将来遇到您的代码的程序员使用。

您可以将其转换为数组

// get your array
$array1 = CALL_FUNCTION();

// cast the stdObject to an array
$array1['Address'] = (array) $array1['Address'] ;

暂无
暂无

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

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