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条)

报告相同问题?

悬赏问题

  • ¥100 关于注册表摄像头和麦克风的问题
  • ¥30 代码本地运行正常,但是TOMCAT部署时闪退
  • ¥15 关于#python#的问题
  • ¥15 主机可以ping通路由器但是连不上网怎么办
  • ¥15 数据库一张以时间排好序的表中,找出多次相邻的那些行
  • ¥50 关于DynamoRIO处理多线程程序时候的问题
  • ¥15 kubeadm部署k8s出错
  • ¥15 Abaqus打不开cae文件怎么办?
  • ¥20 双系统开机引导中windows系统消失问题?
  • ¥15 小程序准备上线,软件开发公司需要提供哪些资料给甲方