[英]How to foreach array whitout get Catchable fatal error in PHP
Hi i have a problem with looping array with foreach. 嗨,我在使用foreach循环数组时遇到问题。 I have this array:
我有这个数组:
stdClass Object
(
[GetAllCitiesResult] => stdClass Object
(
[status] => Success
[returnValue] => SoapVar Object
(
[enc_type] => 0
[enc_value] => stdClass Object
(
[City] => Array
(
[0] => stdClass Object
(
[id] => 7326
[name] => Paris
[postCode] => 7700
)
[1] => stdClass Object
(
[id] => 262
[name] => Berlin
[postCode] => 2932
)
)
)
[enc_stype] => ArrayOfCity
[enc_ns] => http://example.com/services/v1.1
)
)
)
And I try to loop with this PHP code: 我尝试使用此PHP代码进行循环:
echo "<select>";
foreach($result as $citys) {
echo "<option>" . $citys . "</option>";
}
echo "</select>";
When I try this code it show me this error "Catchable fatal error: Object of class stdClass could not be converted to string in C:\\xampp\\htdocs\\test\\index.php on line X" 当我尝试此代码时,它向我显示此错误“可捕获的致命错误:类stdClass的对象无法在X行的C:\\ xampp \\ htdocs \\ test \\ index.php中转换为字符串”
I would like to check the object "status" every time run the code and I would like the object name from array "City" to get in dropdown list. 我想每次运行代码时都要检查对象“状态”,我希望从数组“ City”中获取对象名称以进入下拉列表。
This is happening because your'e trying to print $cities, which isn't a string, it's a stdClass. 发生这种情况是因为您要打印$ cities,它不是字符串,而是stdClass。
You need to navigate through the array until you have City as your initial array and then you can do 您需要在阵列中导航,直到将City作为初始阵列,然后才能执行
foreach($city as $cityRecord){
print '<option>' . $cityRecord['name'] . '</option'>
}
Hopefully that'll help 希望这会有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.