Note that there are some explanatory texts on larger screens.

plurals
  1. POAccessing the properties of the last object in an array of objects?
    text
    copied!<p>Given that you have an array that contains varying amounts of objects, how can you access the properties of the last object? I tried to use <code>end($array);</code> but it gives the error: <code>Object of class Post could not be converted to string</code></p> <p>The class is:</p> <pre><code>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-&gt;datafile); foreach($data as $row) { list($index, $subIndex, $poster, $message, $date) = explode('|', $row); $this-&gt;index = $subIndex; // SubIndex ties a Comment to a Post (same ID) if($this-&gt;index == $givenIndex) { $comment = new Post(); $comment-&gt;poster = $poster; $comment-&gt;message = $message; $comment-&gt;date = date(DATEFORMAT, strtotime($date)); $comments[] = $comment; } } return $comments; } } </code></pre> <p>Now, I would like to access only the last Comment item's properties, but I'm not sure how it should be done? In a regular array, end() is quick and easy to use, but how about with objects as it doesn't seem to work?</p> <p>Here is an example var_dump:</p> <pre><code>array (size=2) 0 =&gt; object(Post)[4] private 'datafile' =&gt; null public 'index' =&gt; null public 'subIndex' =&gt; null public 'poster' =&gt; string 'Postaaja' (length=8) public 'title' =&gt; null public 'message' =&gt; string 'Kommentti' (length=9) public 'date' =&gt; string '5 Mar 2013 | 23:12' (length=18) 1 =&gt; object(Post)[5] private 'datafile' =&gt; null public 'index' =&gt; null public 'subIndex' =&gt; null public 'poster' =&gt; string 'Toinenkin' (length=9) public 'title' =&gt; null public 'message' =&gt; string 'Lisäkommentti' (length=14) public 'date' =&gt; string '5 Mar 2013 | 23:13' (length=18) </code></pre> <p>Thanks!</p> <p>EDIT: Here's the way I tried to use it:</p> <pre><code>$comments = new Post(FILECOMMENTS); $currentComments = $comments-&gt;getCommentData($i); // $i is the index of current newspost item $newsComments = new Format(); $newsComments-&gt;formatShortComment($currentComments, $i); </code></pre> <p>And the method in the Format class:</p> <pre><code>// This comment is displayed as a short text in the main News view public function formatShortComment($data, $index) {?&gt; &lt;div class="newsComments"&gt; &lt;p class="newsPreviewComment"&gt; &lt;?php $lastItem = end($data); if(!empty($lastItem-&gt;message)) { echo '&lt;i&gt;&amp;quot;',$lastItem-&gt;message,'&amp;quot;&lt;/i&gt; '; echo '-',$lastItem-&gt;poster; } ?&gt;&lt;/p&gt; &amp;raquo; &lt;a href="?page=comments&amp;amp;id=&lt;?php echo $index; ?&gt;"&gt;Show All/Add comments&lt;/a&gt; (&lt;?php echo $commentCount; ?&gt;) &lt;/div&gt;&lt;?php } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload