In your comment you said this "array" was decoded from JSON. When you use json_decode
, send true
as the 2nd parameter. That tells it to make arrays instead of objects when decoding.
You're having trouble because the array is being decoded as an object, which you access using ->
instead of []
.
$newArray = json_decode($jsonString, true);
UPDATE: You were trying to do (array)json_decode($jsonString)
and that wasn't working. That's because PHP is silly when it comes to type-casting.
Here's a quote from the PHP docs:
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.
This can result in some unexpected behaviour.
Source: http://php.net/manual/en/language.types.array.php#language.types.array.casting
So, it wasn't working because PHP said so.