PHP says Illegal string offset or Cannot access offset of type string on string
Warning: Illegal string offsetShort answer
You used an array key on a value that is a string. PHP treats $str['name'] as a character position. In PHP 8 a non-numeric key throws a TypeError; in PHP 7 it was a warning. Find the variable that is a string, decode or fix it so it becomes an array, then read the key.
Fix it fast
1Find out which cause you have
Dump the variable right before the line that fails. The type it prints is the answer.
var_dump(gettype($row), $row);2Match what you see, then jump to its fix
- 1
gettype prints string and the content starts with { or [A JSON string or serialized value was never decodedFix - 2
The error only appears inside a foreach, on the first or last passThe loop level is wrong, so each item is a stringFix - 3
Works for some records, fails for othersA value that can be an array or a string is used without a checkFix
- 1
What does the error look like?
Warning: Illegal string offsetWarning: Illegal string offset "1x"Fatal error: Uncaught TypeError: Cannot access offset of type string on string
Cause 1 of 3
A JSON string or serialized value was never decoded
1Check
Data from an API call, a text column or a request body arrives as text. Reading a key from the text means reading a character position.
var_dump(is_string($data), substr($data, 0, 60));2Fix
Decode the text into an array first. In Laravel a model attribute can also be cast to array.
$data = json_decode($data, true); // true = associative array
echo $data['name'];
// Laravel model
protected function casts(): array
{
return ['options' => 'array'];
}3Verify
gettype now prints array, and the key reads without a warning.
var_dump(gettype($data));Confirmed by php.net
Cause 2 of 3
The loop level is wrong, so each item is a string
1Check
Looping over one row instead of a list of rows gives you its fields one by one. Each field can be a plain string, so $item['id'] fails.
foreach ($rows as $item) {
var_dump(gettype($item));
}2Fix
Loop over the list of rows, or read the key from the row directly, not from each field in it.
// wrong: $user is one row, so $field is a string
foreach ($user as $field) { echo $field['name']; }
// right
foreach ($users as $user) { echo $user['name']; }3Verify
Run the loop again with the type check removed.
php -l file.phpConfirmed by php.net
Cause 3 of 3
A value that can be an array or a string is used without a check
1Check
Some data sources return a string for one item and an array for many. In PHP 8 a key such as 'x' throws a TypeError. A key that starts with digits, like '1x', only warns and is read as 1.
var_dump(is_array($value));2Fix
Test the type, and keep the string case separate.
if (is_array($value)) {
$name = $value['name'] ?? null;
} else {
$name = $value;
}3Verify
Run it with both kinds of input.
php -r '$v="abc"; var_dump(is_array($v));'Confirmed by php.net
How do I stop it coming back?
- Add type declarations to function arguments and returns, so a string cannot pass as an array.
- Decode JSON once, at the edge where it enters your code.
- Run a static analyser such as PHPStan; it flags array access on a string.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
PHP: Trying to access array offset on value of type null
Warning: Trying to access array offset on value of type null
You wrote $x['key'] while $x is null (or false or an int).
3 causes, each with a fixBack end
Composer says the gd extension is missing on a Laravel project
composer install or composer update stops on a package that requires ext-gd
Composer refuses to install a package that requires ext-gd when the PHP binary running Composer has no GD extension.
4 causes, each with a fixBack end
composer require maatwebsite/excel fails to install
Composer says the package needs an older PHP than the one installed
Composer refuses maatwebsite/excel when the PHP version or a PHP extension does not match what the package and PhpSpreadsheet need.
3 causes, each with a fixBack end
composer install says the lock file is not compatible and asks for update
Your lock file does not contain a compatible set of packages. Please run composer update.
composer install checked the locked packages against this machine's PHP and extensions and found a conflict.
3 causes, each with a fixBack end
composer require laravel/horizon fails: ext-pcntl is missing
laravel/horizon v1.1.0 requires ext-pcntl * -> the requested PHP extension pcntl is missing from your system
Horizon needs the pcntl extension, and Composer refuses to install it where PHP lacks that extension.
3 causes, each with a fixBack end
composer require laravel/passport stops with 'requirements could not be resolved'
... but these were not loaded, likely because it conflicts with another require.
Composer found no Passport release that fits your project at once.
4 causes, each with a fix