在foreach
循环中,我返回一个数组($followerPosts
)。
foreach($myfollowers['entities'] as $myfollower)
{
$followerPosts=$this->displayPostsAction($myfollower->getFollower());
}
我需要在最后有一个包含所有$followerPosts
数组的大数组。
答案 0 :(得分:1)
您可以在循环之前声明一个数组,然后在每次迭代时使用array_merge
或array_push,这取决于你想做什么
答案 1 :(得分:1)
使用array_merge将所有这些放入一个数组中,如下所示:
$big = array();
foreach($myfollowers['entities'] as $myfollower)
{
$big = array_merge($big, $this->displayPostsAction($myfollower->getFollower()));
}
答案 2 :(得分:1)
$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());
}
答案 3 :(得分:1)
您必须将它们添加到数组中。
$followerPosts = array()
foreach($myfollowers['entities'] as $myfollower)
{
//$followerPosts=$this->displayPostsAction($myfollower->getFollower());
$followerPosts[]=$this->displayPostsAction($myfollower->getFollower());
}
print_r(followerPosts)
答案 4 :(得分:1)
出于此目的,我认为最好的工具是array_map
功能:
$followerPosts = array_map(function($f) {
return $this->displayPostsAction($f->getFollower());
}, $myFollowers['entities']);
var_dump($followerPosts);