Why This Happens
In PHP, strings support bracket access for individual characters like $str[0]. If you try to use nested brackets like $str['key']['subkey'] on a string, PHP cannot treat a character as an array. This often happens when a variable is overwritten from an array to a string unexpectedly.
The Problem
$data = '';
$data['users']['admin'] = 'Alice'; // $data is a string, not an arrayThe Fix
$data = [];
$data['users']['admin'] = 'Alice';Step-by-Step Fix
- 1
Check the variable type
Use var_dump() to inspect the variable. It is likely a string where you expected an array.
- 2
Find where the type changed
Trace the variable's assignments to find where it was set to a string instead of being initialized as an array.
- 3
Initialize as array
Ensure the variable is initialized as an empty array [] before using nested array assignment syntax.
Bugsly catches this automatically
Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.
Try Bugsly free