Cannot Use String Offset as Array

Fatal error: Cannot use string offset as an array

Quick Answer

You are trying to use nested array syntax on a string variable. The variable you think is a nested array is actually a string. Check the variable type and data structure.

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 array

The Fix

$data = [];
$data['users']['admin'] = 'Alice';

Step-by-Step Fix

  1. 1

    Check the variable type

    Use var_dump() to inspect the variable. It is likely a string where you expected an array.

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