doujia1679 2012-10-16 16:52
浏览 35
已采纳

试图获取非对象错误的属性

I'm trying to figure out why I keep getting a few trying to get property of non object errors. with the array that I have created. I created the array and am foreaching it out and by the looks of my object array those properties do exist.

I am getting the errors in this area:

// Separates each of the last 5 pesonal messages
foreach ($last_5_personal_messages AS $message)
{
    echo '<li>';
    echo '<span class="icon avatar"><img src="'.$message->sender_avatar.'" alt="" /></span>';
    echo '<span class="name"><a href="'.$site_url.'/personalmessages/viewmessage/'.$message->message_id.'"><strong>'.$message->sender_name.'</strong></a><span class="time">'.$message->datetime_sent.' ago</span></span>';
    echo '<span class="msg">'.$message->subject.'</span>';
    echo '</li>';
}
?>


Array
(
[0] => stdClass Object
    (
        [message_id] => 1
        [subject] => Test Message
        [datetime_sent] => 2 weeks
        [attachments] => 
        [priority] => 0
        [message_content] => Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
        [message_read] => 0
        [is_favorite] => 0
        [sender_name] => Mike Scott
        [sender_email_address] => myemail@myemail.com
        [sender_avatar] => http://dev.myserver.com/assets/themes/supr/images/avatars/avatar5.jpg
    )

[1] => stdClass Object
    (
        [message_id] => 2
        [subject] => Testing PM Message
        [datetime_sent] => 3 weeks
        [attachments] => Array
            (
                [0] => Array
                    (
                        [file_name] => file1.jpg
                        [is_file] => 1
                        [file_size] => 7.01 KB
                        [file_location] => assets/downloads/file1.jpg
                    )

                [1] => Array
                    (
                        [file_name] => file2.jpg
                        [is_file] => 
                    )

                [2] => Array
                    (
                        [file_name] => file3.jpg
                        [is_file] => 
                    )

            )

        [priority] => 0
        [message_content] => This is jsut a test of the personal message system!
        [message_read] => 0
        [is_favorite] => 0
        [sender_name] => Kevin Scott
        [sender_email_address] => myemail@server.com
        [sender_avatar] => http://dev.myserver.com/assets/themes/supr/images/avatars/avatar5.jpg
    )

[2] => stdClass Object
    (
        [message_id] => 3
        [subject] => Testing Whatever
        [datetime_sent] => 1 week
        [attachments] => 
        [priority] => 0
        [message_content] => Testing Message Content
        [message_read] => 0
        [is_favorite] => 0
        [sender_name] => Frank Scott
        [sender_email_address] => frankscott@testemail.com
        [sender_avatar] => http://dev.myserver.com/assets/themes/supr/images/avatars/avatar.jpg
    )

[total_unread_messages] => 3
)
  • 写回答

2条回答 默认 最新

  • doudou5421 2012-10-16 17:04
    关注

    GBD is right, it's because the total_unread_messages key does not contain an object as its value, so you get this warning.

    You can also iterate over that array manually, using the information within total_unread_messages to your advantage, if you don't want to have that if( is_object()) check on every loop iteration. Something like this should work:

    for( $i = 0; $i < $last_5_personal_messages['total_unread_messages'] && $i < 5; $i++) {
        $message = $last_5_personal_messages[$i];
         echo '<li>';
         // ....
    }
    

    Or you can unset() that value before you iterate (and optionally put it back when you're done):

    unset( $last_5_personal_messages['total_unread_messages']);
    foreach( $last_5_personal_messages as $message) {
        // ...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容