PHPWarningRuntime ErrorMay 30, 2026

Runtime Error

Cannot use object of type stdClass as array in /var/www/html/todo.php on line 28

What This Error Means

This error occurs when you're trying to access an object's property as if it were an array. In PHP, objects and arrays are two different data types, and you can't treat them interchangeably.

Why It Happens

This error typically happens when you're working with APIs or third-party libraries that return data in the form of objects or arrays, but you're not aware of the type of data you're working with. You might also encounter this error when you're trying to iterate over an object using a foreach loop or when you're trying to access an object's properties as if it were an array.

How to Fix It

  1. 1To fix this error, you need to use the correct syntax to access the object's properties. You can use the object's methods or properties directly, or you can use the stdClass object's properties using the object->property syntax.

Example Code Solution

❌ Before (problematic code)
PHP
foreach ($todo as $task) {
  echo $task['name'];
}
✅ After (fixed code)
PHP
foreach ($todo as $task) {
  echo $task->name;
}

Fix for Cannot use object of type stdClass as array in /var/www/html/todo.php on line 28

Related PHP Errors

Related PHP Blog Articles

Have a different error? Get an instant explanation.

Explain Another Error