PHP错误:无法使用类型stdClass的对象为数组(数组和对象的问题) [英] PHP Error: Cannot use object of type stdClass as array (array and object issues)

查看:220
本文介绍了PHP错误:无法使用类型stdClass的对象为数组(数组和对象的问题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图复制此code:

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

和这里是我的code:

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

不能使用类型为stdClass已经数组对象

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

推荐答案

您从一个数组的数组控股的形式使用数据复制的例子,您使用的是一个数组持有对象的表单数据。对象和数组是不一样的,也正因为如此,他们使用不同的语法来访问数据。

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.

如果你不知道的变量名,只是做了的var_dump($博客); 循环中看到他们。

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

最简单的方法 - 直接访问$博客作为一个对象:

尝试(假设这些变量是正确的):

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

另一种方法 - 获得$博客作为一个数组:

另外,你可以把 $博客到一个数组与 get_object_vars documentation ):

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

这是值得一提的是,这并不一定要与嵌套对象工作,所以其可行性完全取决于你的 $博客对象的结构。

比任何更好的上面 - 内联PHP语法

说了这么多,如果你想在最可读的方式来使用PHP,既不以上是正确的。当使用PHP与HTML混合,它被认为最好的做法被许多人使用PHP的替代语法 ,这会减少你的整个code朝九晚四行:

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

希望这有助于。

这篇关于PHP错误:无法使用类型stdClass的对象为数组(数组和对象的问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆