35

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"

Alex
  • 8,461
  • 6
  • 37
  • 49

6 Answers6

79

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.

Glitch Desire
  • 14,632
  • 7
  • 43
  • 55
  • 1
    Yet, the php manuals say you can use the array notation for objects. So, why do we háve to use "->"? – e-motiv Dec 20 '18 at 19:01
  • @e-motiv Yes, I'm facing the same issue currently: https://www.php.net/manual/en/sdo.sample.getset.php – gregn3 Jan 23 '20 at 17:14
36

$blog is an object not an array

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

Anigel
  • 3,435
  • 1
  • 17
  • 23
4

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

$blog->id;
$blog->title;
$blog->content;
Software Guy
  • 3,190
  • 4
  • 21
  • 21
4

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;
chandresh_cool
  • 11,753
  • 3
  • 30
  • 45
4

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.

CashIsClay
  • 2,182
  • 18
  • 18
4

StdClass object is accessed by using ->

foreach ($blogs as $blog) {
    $id         = $blog->id;
    $title  = $blog->title;
    $content    = $blog->content;
}
Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100
  • yes... but i was wondering why cant i copy the code above. like this foreach ($blogs as $blog) { $id = $blog['id']; $title = $blog['title']; $content = $blog['content']; ?>

    – Emmanuel Joseph U Bastero May 16 '13 at 13:57
  • 1
    @EmmanuelJosephUBastero - You can't copy the code because they're working with an array of arrays, and you're working with an array of objects. It's like asking why you can't copy how someone drives a car on a unicycle, they are totally different tools. I explained in my answer how to turn your objects into arrays. – Glitch Desire May 16 '13 at 13:58