我从 CodeIgniter 的控制器中看到以下结果数组:
Array
(
[cities] => Array
(
[0] => stdClass Object
(
[id] => 1
[name] => Jersey City
[state] => stdClass Object
(
[id] => 1
[name] => New Jersey
)
)
[1] => stdClass Object
(
[id] => 2
[name] => Philadelphia
[state] => stdClass Object
(
[id] => 2
[name] => Pennsylvania
)
)
)
)
现在我想用这些值创建一个下拉框,以便该框具有城市、州。
Jersey, New Jersey
Philadelphia, Pennsylvania
这就是我试图实现这一目标的方式:
<?php
foreach($cities as $city) {
echo "<option value='$city->id'>$city->name, $city->state->name</option>";
}
?>
当我尝试访问时:
$city->state->name
我收到以下错误:
<p>Severity: 4096</p>
<p>Message: Object of class stdClass could not be converted to string</p>
如果有人可以指导我从状态数组中获取名称的正确方法,我将不胜感激。
谢谢!