gpt4 book ai didi

php - 为什么我不能直接访问带索引的数组?

转载 作者:可可西里 更新时间:2023-11-01 12:47:04 26 4
gpt4 key购买 nike

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

我有一个 Employee 类的对象,我将其类型转换为数组并尝试像这样显示它:

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

现在我不明白这个 Star(*)。当我的成员变量很简单时,比如 m_UserId、m_EmialPassword 等等,它从哪里得到这颗星。当我尝试使用以下 2 个语句显示相同内容时,我收到了一个错误:-

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

display( $arrOfObj['m_EmailAddress'] );

两者都给出错误消息 Undefined index: m_EmailAddress

当我尝试这样做时它工作正常:-

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

最后一个工作正常,任何人都可以向我解释问题。

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

最佳答案

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

尝试 var_dump(bin2hex($arrKeys[8])) 获得启发。另请参阅上面链接的手册中的示例。

关于php - 为什么我不能直接访问带索引的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12514558/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com