Maybe I have a typo or maybe I don't understand how php variable referencing works.
I have an array : $comments = array_fill(0, $numComments, "0"); and I have a comments Collection filled with Comment objects and each comment object might have a children property that contains a collection of comments: $commentsCollection = MyModel::getComments();
I am getting the comments collection fine. When I dd it, it looks good. So I want to fill the array, $comments where the key is the comment's id and the value is the Comment object.
So I wrote this recursive function:
private function fillArrayWithComments(array $commentsArray, $commentsCollection){
foreach($commentsCollection as $comment){
if ($comment->children){
$this->fillArrayWithComments($commentsArray, $comment->children);
}
$commentsArray[$comment->id] = $comment;
//dd($commentsArray);
}
}
and I call it like this $this->fillArrayWithComments($comments, $commentsCollection);
when I uncomment the dd code in the fillArrayWithComments above, then I see that it puts the correct object at the right location. but when I do dd($comments); after the function has run then I get my original array, as if it was never updated. Why is this? what am i doing wrong?