Inside a foreach
loop, I am returning an array ($followerPosts
).
foreach($myfollowers['entities'] as $myfollower)
{
$followerPosts=$this->displayPostsAction($myfollower->getFollower());
}
I need to have at the end one one big array containing all the $followerPosts
arrays.
$bigArray = array();
foreach($myfollowers['entities'] as $myfollower)
{
$followerPosts=$this->displayPostsAction($myfollower->getFollower());
$bigArray[] = $followerPosts;
}
OR
$bigArray = array();
foreach($myfollowers['entities'] as $myfollower)
{
$bigArray[] =$this->displayPostsAction($myfollower->getFollower());
}
You can declare an array before the loop, then use array_merge on each iteration
or array_push, it depends on what you want to do
Use array_merge to put all of them into one array like this:
$big = array();
foreach($myfollowers['entities'] as $myfollower)
{
$big = array_merge($big, $this->displayPostsAction($myfollower->getFollower()));
}
You have to add them to the array.
$followerPosts = array()
foreach($myfollowers['entities'] as $myfollower)
{
//$followerPosts=$this->displayPostsAction($myfollower->getFollower());
$followerPosts[]=$this->displayPostsAction($myfollower->getFollower());
}
print_r(followerPosts)
For the purpose the best tool, I think, is the array_map
function:
$followerPosts = array_map(function($f) {
return $this->displayPostsAction($f->getFollower());
}, $myFollowers['entities']);
var_dump($followerPosts);