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

时间:2012-10-16 16:52:28

标签: php

我正在试图找出为什么我不断尝试获取非对象错误的属性。与我创建的数组。我创建了数组并且正在进行预测,并且通过我的对象数组的外观,这些属性确实存在。

我收到了这方面的错误:

// 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 个答案:

答案 0 :(得分:1)

因为数组中的键“total_unread_messages”没有对象作为值,所以在foreach中跳过该值

foreach ($last_5_personal_messages AS $message)
{
    if(!is_object($message)){
      continue;
    }
    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>';

}

另一种方式

for($i=0;$i<$last_5_personal_messages['total_unread_messages'];$i++)
{
   $message = $last_5_personal_messages[$i];

    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>';
}

答案 1 :(得分:1)

GBD是对的,这是因为total_unread_messages键不包含对象作为其值,因此您会收到此警告。

如果您不想在每次循环迭代中检查total_unread_messages,您也可以使用if( is_object())中的信息手动迭代该数组。这样的事情应该有效:

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

或者您可以在迭代之前unset()该值(并且在您完成后可选地将其放回):

unset( $last_5_personal_messages['total_unread_messages']);
foreach( $last_5_personal_messages as $message) {
    // ...
}