1

さまざまな量のオブジェクトを含む配列がある場合、最後のオブジェクトのプロパティにアクセスするにはどうすればよいでしょうか? 使用しようとしまし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
}
4

2 に答える 2

0

あなたは試すことができます:

$tempArray = array_values($data);

$lastItem = $tempArray[count($tempArray)-1];
于 2013-03-13T18:40:27.813 に答える
0

重要なことを見落としていないことを願っていますが、PHP 配列の最後の要素を取得しようとしている場合は、次のようになります。

$lastItem = $data[count($data) - 1];
于 2013-03-13T18:49:46.067 に答える