【问题标题】:Accessing the properties of the last object in an array of objects?访问对象数组中最后一个对象的属性?
【发布时间】:2013-03-01 20:45:48
【问题描述】:

假设您有一个包含不同数量对象的数组,您如何访问最后一个对象的属性?我尝试使用end($array);,但它给出了错误:Object of class Post could not be converted to string

班级是:

class Post {
        private $datafile;
        public $index;
        public $subIndex;
        public $poster;
        public $title;
        public $message;
        public $date;

// constructor and some unrelated methods here

        public function getCommentData($givenIndex) {
            $comments = null;
            $data = file($this->datafile);
            foreach($data as $row) {
                list($index, $subIndex, $poster, $message, $date) = explode('|', $row);
                $this->index = $subIndex; // SubIndex ties a Comment to a Post (same ID)
                if($this->index == $givenIndex) {
                    $comment = new Post();
                    $comment->poster = $poster;
                    $comment->message = $message;
                    $comment->date = date(DATEFORMAT, strtotime($date));
                    $comments[] = $comment;
                }
            }
            return $comments;
        }
}

现在,我只想访问最后一个评论项的属性,但我不知道应该怎么做?在常规数组中,end() 快速且易于使用,但是对于似乎不起作用的对象呢?

这是一个示例 var_dump:

array (size=2)
  0 => 
    object(Post)[4]
      private 'datafile' => null
      public 'index' => null
      public 'subIndex' => null
      public 'poster' => string 'Postaaja' (length=8)
      public 'title' => null
      public 'message' => string 'Kommentti' (length=9)
      public 'date' => string '5 Mar 2013 | 23:12' (length=18)
  1 => 
    object(Post)[5]
      private 'datafile' => null
      public 'index' => null
      public 'subIndex' => null
      public 'poster' => string 'Toinenkin' (length=9)
      public 'title' => null
      public 'message' => string 'Lisäkommentti' (length=14)
      public 'date' => string '5 Mar 2013 | 23:13' (length=18)

谢谢!

编辑: 这是我尝试使用它的方式:

$comments = new Post(FILECOMMENTS);
$currentComments = $comments->getCommentData($i); // $i is the index of current newspost item

$newsComments = new Format();
$newsComments->formatShortComment($currentComments, $i);

以及Format类中的方法:

// This comment is displayed as a short text in the main News view
public function formatShortComment($data, $index) {?>
    <div class="newsComments">
        <p class="newsPreviewComment">
        <?php 
            $lastItem = end($data);
            if(!empty($lastItem->message)) {
                echo '<i>&quot;',$lastItem->message,'&quot;</i> ';
                echo '-',$lastItem->poster;
            }
        ?></p>
        &raquo; <a href="?page=comments&amp;id=<?php echo $index; ?>">Show All/Add comments</a>
        (<?php echo $commentCount; ?>)
    </div><?php
}

【问题讨论】:

  • 请贴出产生错误的代码。根据描述和此处显示的内容,理论上它应该可以工作,但如果没有导致问题的代码,则无法确定。
  • 如何使用 array_pop($arrayofObjects);
  • @Adrian:我在帖子中添加了代码
  • 感谢您更新代码 - 哪一行触发了您原始帖子中提到的错误?
  • @Adrian:$lastItem = end($data); 部分触发错误

标签: php arrays oop


【解决方案1】:

你可以试试:

$tempArray = array_values($data);

$lastItem = $tempArray[count($tempArray)-1];

【讨论】:

    【解决方案2】:

    我希望我没有遗漏一些重要的东西,但如果你只是想获取 PHP 数组的最后一个元素:

    $lastItem = $data[count($data) - 1];
    

    【讨论】:

      猜你喜欢
      • 2018-03-16
      • 2013-12-02
      • 1970-01-01
      • 1970-01-01
      • 2015-08-03
      • 1970-01-01
      • 2021-01-11
      • 1970-01-01
      相关资源
      最近更新 更多