When you cast (object)
in an array, you advertise this array as a list of the internal properties of an anonymous object (i.e. stdClass).
How properties are indexed in an object is slightly different from array properties; in particular, property names of objects are always treated as strings, while array indices are scanned based on the expected type (for example, numeric strings are treated as integers).
The above behavior does not affect foreach
loops because there is no hashing; as for PHP, a regular array is repeated.
To answer your question, yes, using the ->
operator it ->
not possible to get numeric keys from the original array. To avoid this, you must remove the numerical indices from your array before doing the translation.
It's hard to find this behavior in the documentation, but a hint of it can be found here :
If the object is converted to an array, the result is an array whose elements are the properties of the object. Keys are member variable names with a few notable exceptions: integer properties are not available ...
Fyi
In this particular case, you can work around the problem using links; this is not recommended, please follow the directions below without using numeric property names:
foreach ($a as &$c) { ++$c; } unset($c);
Update
2014-11-26: I updated the documentation; live pages will be updated this Friday - commit .
Ja͢ck source share