为什么不能直接使用索引访问数组? [英] Why can't I access the array with index directly?

查看:281
本文介绍了为什么不能直接使用索引访问数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试直接使用其索引访问数组元素时,我感到困惑.我想我可以在编码中更好地解释它:-

I got confused when I was trying to access an array element directly with its index. I guess I could explain it better in coding:-

我有一个Employee Class对象,我对它进行TypeCast数组化,并尝试显示如下:

I am having an object of Employee Class and I TypeCast it to array and tried to display it like this:

$arrOfObj = (array) $objEmployee;

$arrKeys = array_keys( $arrOfObj );

display( $arrOfObj ); // display() is a method in my library that prints an array in a mannered way.

这给了我以下输出:-

Array
(
   [*m_UserId] => 1155
   [*m_EmailPassword] => 
   [*m_IsAssignedToManagementCompany] => 
   [*m_ManagementCompanyId] => 
   [*m_DepartmentId] => 3
   [*m_DesignationId] => 4
   [*m_EmployeeCompletedMonth] => 
   [*m_EmployeeCompletedDay] => 
   [*m_EmailAddress] =>showket.mca@gmail.com
   ------
   ------
 )

现在我不明白这个星号(*).当我的成员变量很简单(例如m_UserId,m_EmialPassword等)时,它将从何处获得此星标.当我尝试使用以下2条语句显示相同内容时,出现错误:-

Now here I dont understand this Star(*). when my member variables are simple like m_UserId, m_EmialPassword and So on where from it gets this Star. and when I try to display the same with following 2 statements I got an error :-

display( $arrOfObj['*m_EmailAddress'] );

display( $arrOfObj['m_EmailAddress'] );

均提供错误消息未定义索引:m_EmailAddress

当我尝试以这种方式进行操作时,它可以正常工作:-

And when i try to do it this way It works fine :-

display( $arrOfObj[$arrKeys[8]] );

最后一个很好,任何人都可以向我解释这个问题.

The last one works fine, Can anyone explain me the problem.

            display( $arrOfObj[$arrKeys[11]] );
            display( $arrOfObj['m_strEmailAddress'] );

推荐答案

如果将对象转换为数组,则结果是一个数组,其元素是对象的属性.键是成员变量名称,但有一些值得注意的例外:整数属性不可访问;私有变量的类名在变量名之前; 受保护的变量在变量名前带有"*".这些前置值的两边都为空字节.

http://php.net/manual/zh-CN/language.types.array.php#language.types.array.casting

请尝试var_dump(bin2hex($arrKeys[8]))进行启发.另请参见上面链接的手册中的示例.

Try var_dump(bin2hex($arrKeys[8])) for enlightenment. Also see the example in the above linked manual.

这篇关于为什么不能直接使用索引访问数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆