Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

arrays

object

php

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"

like image 946
Emmanuel Joseph U Bastero Avatar asked May 16 '13 13:05

Emmanuel Joseph U Bastero


2 Answers

The example you copied from is using data in the form of an array holding arrays, you are using data in the form of an array holding objects. Objects and arrays are not the same, and because of this they use different syntaxes for accessing data.

If you don't know the variable names, just do a var_dump($blog); within the loop to see them.

The simplest method - access $blog as an object directly:

Try (assuming those variables are correct):

<?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 } ?> 

The alternative method - access $blog as an array:

Alternatively, you may be able to turn $blog into an array with get_object_vars (documentation):

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

It's worth mentioning that this isn't necessarily going to work with nested objects so its viability entirely depends on the structure of your $blog object.

Better than either of the above - Inline PHP Syntax

Having said all that, if you want to use PHP in the most readable way, neither of the above are right. When using PHP intermixed with HTML, it's considered best practice by many to use PHP's alternative syntax, this would reduce your whole code from nine to four lines:

<?php foreach($blogs as $blog): ?>     <h1><?php echo $blog->title; ?></h1>     <p><?php echo $blog->content; ?></p> <?php endforeach; ?> 

Hope this helped.

like image 134
Glitch Desire Avatar answered Sep 28 '22 18:09

Glitch Desire


$blog is an object not an array

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

like image 35
Anigel Avatar answered Sep 28 '22 18:09

Anigel