comments[] = new Comment($author, 'foo'); $this->comments[] = new Comment($author, 'bar'); } public function getCommentsIndexedByAuthor() { $indexedComments = []; foreach ($this->comments as $comment) { $authorName = $comment->getAuthor()->getName(); if (!isset($indexedComments[$authorName])) { $indexedComments[$authorName] = new IndexedCommentsList(); } $indexedComments[$authorName]->addComment($comment); } return $indexedComments; } } class IndexedCommentsList { /** @XmlList(inline=true, entry="comment") */ private $comments = []; /** @XmlAttribute */ private $count = 0; public function addComment(Comment $comment) { $this->comments[] = $comment; $this->count += 1; } }