[php] PHP Error: Cannot use object of type stdClass as array (array and object issues)

I was trying to copy this code:

<?php
foreach ($products as $product) {
    $id          = $product['id'];
    $name        = $product['name'];
    $description = $product['description'];
    $price       = $product['price'];
?>
    <tr>
    <td><img src="<?php echo $product['picture']; ?>" /></td>
        <td><b><?php echo $name; ?></b><br />
        <?php echo $description; ?><br />
          Price:<big style="color:green">
          $<?php echo $price; ?></big><br /><br />
<?php
    echo form_open('cart/add');
    echo form_hidden('id', $id);
    echo form_hidden('name', $name);
    echo form_hidden('price', $price);
    echo form_submit('action', 'Add to Cart');
    echo form_close();
?>
    </td>
    </tr>
    <tr><td colspan="2"><hr size="1" /></td>
<?php
}
?>

and here is my code:

<?php
   foreach ($blogs as $blog) {
      $id      = $blog['id'];
      $title   = $blog['title'];
      $content = $blog['content'];
?>
      <h1><?php echo $title; ?></h1>
      <h1> <?php echo $content; ?> </h1>

<?php
}
?>

I get this error every time I run my code: "Cannot use object of type stdClass as array"

This question is related to php arrays object

The answer is


There might two issues

1) $blogs may be a stdObject

or

2) The properties of the array might be the stdObject

Try using var_dump($blogs) and see the actual problem if the properties of array have stdObject try like this

$blog->id;
$blog->content;
$blog->title;

If you're iterating over an object instead of an array, you'll need to access the properties using:

$id = $blog->id;
$title = $blog->title;
$content = $blog->content;

That, or change your object to an array.


$blog is an object, not an array, so you should access it like so:

$blog->id;
$blog->title;
$blog->content;

StdClass object is accessed by using ->

foreach ($blogs as $blog) {
    $id         = $blog->id;
    $title  = $blog->title;
    $content    = $blog->content;
}

$blog is an object not an array

try using $blog->id instead of $blog['id']


Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to arrays

PHP array value passes to next row Use NSInteger as array index How do I show a message in the foreach loop? Objects are not valid as a React child. If you meant to render a collection of children, use an array instead Iterating over arrays in Python 3 Best way to "push" into C# array Sort Array of object by object field in Angular 6 Checking for duplicate strings in JavaScript array what does numpy ndarray shape do? How to round a numpy array?

Examples related to object

How to update an "array of objects" with Firestore? how to remove json object key and value.? Cast object to interface in TypeScript Angular 4 default radio button checked by default How to use Object.values with typescript? How to map an array of objects in React How to group an array of objects by key push object into array Add property to an array of objects access key and value of object using *ngFor