Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

PHP says Illegal string offset or Cannot access offset of type string on string

ErrorWarning: Illegal string offset

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

  • Applies to PHP 8.3, PHP 8.4, Laravel 12
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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);
  2. 2Match what you see, then jump to its fix

    1. 1gettype prints string and the content starts with { or [A JSON string or serialized value was never decodedFix
    2. 2The error only appears inside a foreach, on the first or last passThe loop level is wrong, so each item is a stringFix
    3. 3Works for some records, fails for othersA value that can be an array or a string is used without a checkFix

What does the error look like?

  • Warning: Illegal string offset
  • Warning: 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

Did this fix it?

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

Confirmed by php.net

Did this fix it?

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

Did this fix it?

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

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 fix

Back 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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.