PHP Arrays - 试图获取非对象的属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5616776/
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 22:00:28  来源:igfitidea点击:

PHP Arrays - Trying to get property of non-object

phphtmlarrays

提问by Aero Chocolate

I am still new to PHP so please bear with me.

我还是 PHP 新手,所以请耐心等待。

So I am getting this error: Notice: Trying to get property of non-object on this line:

所以我收到这个错误:注意:试图在这一行获取非对象的属性:

echo (
            "<tr>".
            "<td>".$row->last_name.     "</td>".
            "<td>".$row->first_name.    "</td>".
            "<td>".$row->phone_no.      "</td>".
            "<td>".$row->date_of_birth. "</td>".
            "<td>".$row->membership.    "</td>".
            "</tr></table>");

I've used print_r on my function and I get:

我在我的函数上使用了 print_r,我得到:

Array
(
    [0] => Array
    (
        [0] => Lee
        [last_name] => Lee
        [1] => Lian
        [first_name] => Lian
        [2] => 39025823
        [phone_no] => 39025823
        [3] => 1967-09-19
        [date_of_birth] => 1967-09-19
        [4] => T
        [membership] => T
        [5] =>
        [status] =>
        [6] => 0
        [room_no] => 0
    )
)

So there are elements within the array.

所以数组中有元素。

The actual code falls under:

实际代码属于:

foreach($array as $row)
    {
            echo (
            "<tr>".
            "<td>".$row->last_name.     "</td>".
            "<td>".$row->first_name.    "</td>".
            "<td>".$row->phone_no.      "</td>".
            "<td>".$row->date_of_birth. "</td>".
            "<td>".$row->membership.    "</td>".
            "</tr></table>");
    }

I was thinking - how would I convert an array into an object? Maybe that would be my solution.

我在想 - 如何将数组转换为对象?也许这就是我的解决方案。

回答by Phil

I was thinking - how would I convert an array into an object? Maybe that would be my solution.

我在想 - 如何将数组转换为对象?也许这就是我的解决方案。

That indeed would be one solution.

这确实是一种解决方案。

$row = (object) $row;

Another would be to use the right syntax for the data-type in question, in this case an array.

另一种方法是对所讨论的数据类型使用正确的语法,在这种情况下是数组。

Instead of

代替

$row->last_name

You should use

你应该使用

$row['last_name']

回答by Pascal MARTIN

As you are working with an array, you should use []to access the array's items :

当您使用数组时,您应该使用[]访问数组的项:

echo $row['last_name'];

Use the right syntax, and the error will go away ;-)

使用正确的语法,错误就会消失;-)


Still, if you really want to convert an array to an object (not really sure why you'd do that, though, in this specific case), you can use this :


尽管如此,如果您真的想将数组转换为对象(但不确定为什么要这样做,但在这种特定情况下),您可以使用以下命令:

$row = (object)$row;
echo $row->last_name;

Here's the relevant section of the manual : Type Casting

这是手册的相关部分:Type Casting

回答by Anthony Hyman

Try this...

尝试这个...

foreach($array as $row)
{
    echo (
        "<tr>".
        "<td>".$row['last_name'].     "</td>".
        "<td>".$row['first_name'].    "</td>".
        "<td>".$row['phone_no'].      "</td>".
        "<td>".$row['date_of_birth']. "</td>".
        "<td>".$row['membership'].    "</td>".
        "</tr></table>");
}