无法在php中访问数组中的特殊键

时间:2011-09-24 19:19:43

标签: php arrays

当我使用以下代码时

foreach($admin_info as $key => $val)
{
echo $key."   ".$val."\n";
}
我会得到真实的结果:

id 1
username Moein7tl
password 0937afa17f4dc08f3c0e5dc908158370ce64df86
mail moein7tl@gmail.com
added_time 
super_admin 
last_time 
last_ip 
see_user_per 1
change_user_per 1
see_people_per 1
change_people_per 1
add_people_per 1
remove_people_per 1
see_album_per 1
add_album_per 1
change_album_per 1
remove_album_per 1
see_music_per 1
add_music_per 1
change_music_per 1
remove_music_per 1
admin_per 1
yahoo_per 1
status_per 1
pm_per 1
ip_blocking_per 1

但是当我使用echo ($admin_info['id']);或其他键时,它会出错并且不会显示任何结果。 哪里有问题? - 编辑 var_dump($admin_info);将返回

object(stdClass)#21 (27) { ["id"]=> string(1) "1" ["username"]=> string(8) "Moein7tl" ["password"]=> string(40) "0937afa17f4dc08f3c0e5dc908158370ce64df86" ["mail"]=> string(18) "moein7tl@gmail.com" ["added_time"]=> NULL ["super_admin"]=> string(0) "" ["last_time"]=> NULL ["last_ip"]=> NULL ["see_user_per"]=> string(1) "1" ["change_user_per"]=> string(1) "1" ["see_people_per"]=> string(1) "1" ["change_people_per"]=> string(1) "1" ["add_people_per"]=> string(1) "1" ["remove_people_per"]=> string(1) "1" ["see_album_per"]=> string(1) "1" ["add_album_per"]=> string(1) "1" ["change_album_per"]=> string(1) "1" ["remove_album_per"]=> string(1) "1" ["see_music_per"]=> string(1) "1" ["add_music_per"]=> string(1) "1" ["change_music_per"]=> string(1) "1" ["remove_music_per"]=> string(1) "1" ["admin_per"]=> string(1) "1" ["yahoo_per"]=> string(1) "1" ["status_per"]=> string(1) "1" ["pm_per"]=> string(1) "1" ["ip_blocking_per"]=> string(1) "1" }

3 个答案:

答案 0 :(得分:0)

我发现问题,CodeIgniter会将数据库结果作为Object返回。 所以我应该像$admin_info->id那样做,它解决了问题。

答案 1 :(得分:0)

尝试$admin_info->password,这应该有效,因为$admin_info似乎是stdClass的对象。

答案 2 :(得分:-3)

您的逻辑是正确的,但您可以尝试使用echo $ admin_info ['id'];没有括号。