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>
状態配列から名前を取得する正しい方法を教えていただければ幸いです。
ありがとう!