EDITED:
I am generating a 2D array and storing it in db as json string. When I need to modify anything in the array, I then fetch the json string and decode it like
$myarray = (array)json_decode($jsonString);
The dump of array is as

$index = 2;
When I wan to access object at index ‘2’ like $myarray[$index] I get null. Please guide what I am doing wrong?
In your comment you said this “array” was decoded from JSON. When you use
json_decode, sendtrueas 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[].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:
Source: http://php.net/manual/en/language.types.array.php#language.types.array.casting
So, it wasn’t working because PHP said so.