PHP阵列 - 试图让非对象的属性 [英] PHP Arrays - Trying to get property of non-object

查看:129
本文介绍了PHP阵列 - 试图让非对象的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还是新的PHP所以请多多包涵。

所以,我得到这个错误:
注意:试图让该行非对象财产:

 回声(
            &所述; TR>中。
            < TD>中。$行向>姓氏。 &所述; / TD>中。
            < TD>中。$行向> FIRST_NAME。 &所述; / TD>中。
            &所述; TD>中。$行级别> phone_no。 &所述; / TD>中。
            &所述; TD>中。$行级别> DATE_OF_BIRTH。 &所述; / TD>中。
            < TD>中。$行向>成员。 &所述; / TD>中。
            < / TR>< /表>);

我用的print_r对我的作用,我也得到:

 阵列

    [0] =>排列
    (
        [0] =>背风处
        [姓氏] =>背风处
        [1] =>廉
        [FIRST_NAME] =>廉
        [2] => 39025823
        [phone_no] => 39025823
        [3] => 1967年9月19日
        [DATE_OF_BIRTH] => 1967年9月19日
        [4] => ŧ
        [会员] => ŧ
        [5] =>
        [现状] =>
        [6] => 0
        [room_no] => 0
    )

因此​​,有在阵列中的元素

实际code瀑布下的:

 的foreach($数组作为$行)
    {
            回声(
            &所述; TR>中。
            < TD>中。$行向>姓氏。 &所述; / TD>中。
            < TD>中。$行向> FIRST_NAME。 &所述; / TD>中。
            &所述; TD>中。$行级别> phone_no。 &所述; / TD>中。
            &所述; TD>中。$行级别> DATE_OF_BIRTH。 &所述; / TD>中。
            < TD>中。$行向>成员。 &所述; / TD>中。
            < / TR>< /表>);
    }

我在想 - 我将如何转换一个数组对象?也许这将是我的解决办法。


解决方案

  

我在想 - 我将如何转换一个数组对象?也许这将是我的解决办法。


这确实是一种解决方案。

  $行=(对象)$行;

另一个办法是使用正确的语法数据类型中的问题,在这种情况下,阵列

而不是

  $行向>姓氏

您应该使用

  $行['姓氏']

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

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:

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.

解决方案

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']

这篇关于PHP阵列 - 试图让非对象的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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