Php 回显对象数据并在下拉列表中显示
我正在尝试回显当前用户的字符\ u名称。如何获得这一点,需要使用默认的字符id,遍历字符列表并获取该字符的名称。然后在下面显示一个下拉菜单,其中包含characters属性内的其他字符 关于如何实现这一点有什么想法吗Php 回显对象数据并在下拉列表中显示,php,Php,我正在尝试回显当前用户的字符\ u名称。如何获得这一点,需要使用默认的字符id,遍历字符列表并获取该字符的名称。然后在下面显示一个下拉菜单,其中包含characters属性内的其他字符 关于如何实现这一点有什么想法吗 object(stdClass)#26 (18) { ["user_id"]=> string(5) "10004" ["role_id"]=> string(1) "4" ["default_character_id"]=> string
object(stdClass)#26 (18) {
["user_id"]=>
string(5) "10004"
["role_id"]=>
string(1) "4"
["default_character_id"]=>
string(1) "1"
["characters"]=>
array(3) {
[0]=>
object(stdClass)#30 (6) {
["id"]=>
string(1) "3"
["character_name"]=>
string(5) "twilliams"
["user_id"]=>
string(5) "10004"
}
[1]=>
object(stdClass)#31 (6) {
["id"]=>
string(1) "4"
["character_name"]=>
string(4) "ksmith"
["user_id"]=>
string(1) "0"
}
[2]=>
object(stdClass)#32 (6) {
["id"]=>
string(1) "5"
["character_name"]=>
string(11) "tjones"
["user_id"]=>
string(1) "0"
}
}
}
<a href="" data-toggle="dropdown" class="glyphicons cogwheel">
<i></i>
<?php
//echo character name of logged in user
?>
<span class="caret"></span>
</a>
<ul class="dropdown-menu pull-right">
<li><a href="">Some option</a></li>
<li><a href="">Some other option</a></li>
<li><a href="">Other option</a></li>
</ul>
object(stdClass)#26(18){
[“用户id”]=>
串(5)“10004”
[“角色id”]=>
字符串(1)“4”
[“默认字符\u id”]=>
字符串(1)“1”
[“字符”]=>
阵列(3){
[0]=>
对象(标准类)#30(6){
[“id”]=>
字符串(1)“3”
[“字符名称”]=>
弦(5)“斜纹布”
[“用户id”]=>
串(5)“10004”
}
[1]=>
对象(标准类)#31(6){
[“id”]=>
字符串(1)“4”
[“字符名称”]=>
字符串(4)“ksmith”
[“用户id”]=>
字符串(1)“0”
}
[2]=>
对象(标准类)#32(6){
[“id”]=>
字符串(1)“5”
[“字符名称”]=>
字符串(11)“tjones”
[“用户id”]=>
字符串(1)“0”
}
}
}
这将显示id,尽管我希望显示帖子中所述的名称。@user2576961更新了答案。这给了我:致命错误:无法使用stdClass类型的对象作为数组有其他想法吗?而不是复制和粘贴并期望它能工作。。您是否有可能更改变量名以匹配您的变量名?我确定$object
不是正确的变量名:-)
<a href="" data-toggle="dropdown" class="glyphicons cogwheel">
<i></i>
<?php
echo $object->characters[$object->default_character_id]->charachter_name;
?>
<span class="caret"></span>
</a>
<ul class="dropdown-menu pull-right">
<li><a href="">Some option</a></li>
<li><a href="">Some other option</a></li>
<li><a href="">Other option</a></li>
</ul>