Why can't I directly access the array with index?

I got confused when trying to access an element of an array directly with its index. I think I could better explain this in encoding: -

I have an object of the Employee class, and I TypeCast its array and tried to display it as follows:

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

 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 I do not understand this Star (*). when my member variables are simple, like m_UserId, m_EmialPassword, etc., where does this Star get from it. and when I try to display the same with the following two statements, I got an error: -

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

or

 display( $arrOfObj['m_EmailAddress'] ); 

Both give an undefined index error message : m_EmailAddress

And when I try to do it this way, it works fine: -

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

The latter works just fine, can anyone explain the problem to me.

  display( $arrOfObj[$arrKeys[11]] ); display( $arrOfObj['m_strEmailAddress'] ); 
+7
source share
1 answer

If the object is converted to an array, the result is an array whose elements are the properties of the object. Keys are the names of member variables, with a few notable exceptions: integer properties are not available; private variables have a class name appended to the variable name; protected variables have the value '*' added to the variable name. These preliminary values ​​have zero bytes on both sides.

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 guide.

+14
source

All Articles