douhang1913 2012-09-20 14:14
浏览 26
已采纳

为什么我不能直接使用索引访问数组?

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:-

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.

this gives me the following output :-

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
   ------
   ------
 )

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'] );

or

display( $arrOfObj['m_EmailAddress'] );

Both give the Error message Undefined index: 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'] );
  • 写回答

1条回答 默认 最新

  • dongxun1244 2012-09-20 14:20
    关注

    If an object is converted to an array, the result is an array whose elements are the object's properties. The keys are the member variable names, with a few notable exceptions: integer properties are unaccessible; private variables have the class name prepended to the variable name; protected variables have a '*' prepended to the variable name. These prepended values have null bytes on either side.

    http://php.net/manual/en/language.types.array.php#language.types.array.casting

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ROS Turtlebot3 多机协同自主探索环境时遇到的多机任务分配问题,explore节点
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题