[英]how to retrieve a stdClass Object Array thats located inside another array
I've got an array Address
located inside an array $array1 我有一个位于数组$ array1内的数组
Address
[Address] => stdClass Object
(
[Address1] => xxxxxx
[City] => xxxxx
[State] => xxxxx
[Zip] => xxxxxxx
)
I attempted to call print_r($array1['Address']) but that doesnt work. 我试图调用print_r($ array1 ['Address']),但是没有用。 Does the stdClass Object require that I call it differently?
stdClass对象是否要求我以不同的方式调用它?
I've got $array1
being set up inside a function called CALL_FUNCTION
. 我在名为
CALL_FUNCTION
的函数中设置了$array1
。 Inside the function, I use return $array1
to get access to the array outside the function. 在函数内部,我使用
return $array1
来访问函数外部的数组。
Outside the function, I've got this... 在函数之外,我有这个...
$array1 = array();
$array1 = CALL_FUNCTION();
If I do print_r($array1)
it returns the array with the above std object array inside of it. 如果我执行
print_r($array1)
则返回其中具有上述std对象数组的数组。
$array1 is not an array, it's also an object as indicated by the error: $ array1不是数组,它还是错误所指示的对象:
Fatal error: Uncaught Error: Cannot use object of type stdClass as array
致命错误:未捕获错误:无法将stdClass类型的对象用作数组
Having $array1 = array()
doesn't do anything since you aren't using $array1
and are instantly reassigning it to the return value of CALL_FUNCTION()
. 使用
$array1 = array()
不会执行任何操作,因为您没有使用$array1
,而是立即将其重新分配给CALL_FUNCTION()
的返回值。
Access it as an object: $array1->Address->Address1
. 作为对象访问它:
$array1->Address->Address1
。
However, you'll want to change the name of $array1
to something like $userObject
for the sake of future programmers who come across your code. 但是,您将需要将
$array1
的名称更改为$userObject
类的名称,以便将来遇到您的代码的程序员使用。
You can cast it to an array 您可以将其转换为数组
// 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.