PHP 错误:无法使用 stdClass 类型的对象作为数组(数组和对象问题)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16589313/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:24:02  来源:igfitidea点击:

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

phparraysobject

提问by Emmanuel Joseph U Bastero

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"

每次运行我的代码时都会收到此错误:“无法将 stdClass 类型的对象用作数组”

回答by Glitch Desire

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.

如果您不知道变量名称,只需var_dump($blog);在循环中执行 a即可查看它们。

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

最简单的方法——直接将 $blog 作为对象访问:

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:

另一种方法 - 将 $blog 作为数组访问:

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

或者,您可以$blog使用get_object_vars文档)转换为数组:

<?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 $blogobject.

值得一提的是,这不一定适用于嵌套对象,因此其可行性完全取决于$blog对象的结构。

Better than either of the above - Inline PHP Syntax

比以上任何一个都好 - 内联 PHP 语法

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,以上两种方法都不对。将 PHP 与 HTML 混合使用时,许多人认为使用 PHP 的替代语法是最佳实践,这会将您的整个代码从九行减少到四行:

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

Hope this helped.

希望这有帮助。

回答by Anigel

$blog is an object not an array

$blog 是一个对象而不是一个数组

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

尝试使用$blog->id代替$blog['id']

回答by CashIsClay

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.

那,或者将您的对象更改为数组。

回答by Software Guy

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

$blog 是一个对象,而不是一个数组,所以你应该像这样访问它:

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

回答by chandresh_cool

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

尝试使用 var_dump($blogs) 并查看实际问题,如果数组的属性具有 stdObject 尝试像这样

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

回答by Yogesh Suthar

StdClass object is accessed by using ->

通过使用访问 StdClass 对象 ->

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