0

foreach循环中,我返回一个数组 ( $followerPosts)。

foreach($myfollowers['entities'] as $myfollower)
{
     $followerPosts=$this->displayPostsAction($myfollower->getFollower());
}

最后我需要一个包含所有数组的大$followerPosts数组。

4

5 回答 5

1

使用array_merge将它们全部放入一个数组中,如下所示:

$big = array();
foreach($myfollowers['entities'] as $myfollower)
{
     $big = array_merge($big, $this->displayPostsAction($myfollower->getFollower()));
}
于 2013-08-30T12:31:19.410 回答
1
$bigArray = array();
foreach($myfollowers['entities'] as $myfollower)
{
     $followerPosts=$this->displayPostsAction($myfollower->getFollower());
     $bigArray[] =  $followerPosts;
}

或者

 $bigArray = array();
    foreach($myfollowers['entities'] as $myfollower)
    {
         $bigArray[] =$this->displayPostsAction($myfollower->getFollower());

    }
于 2013-08-30T12:32:43.433 回答
1

您可以在循环之前声明一个数组,然后在每次迭代中使用 array_merge

或array_push,这取决于你想做什么

于 2013-08-30T12:31:07.210 回答
1

您必须将它们添加到数组中。

$followerPosts = array()

foreach($myfollowers['entities'] as $myfollower)
{
     //$followerPosts=$this->displayPostsAction($myfollower->getFollower());
     $followerPosts[]=$this->displayPostsAction($myfollower->getFollower());

}

print_r(followerPosts)
于 2013-08-30T12:34:42.427 回答
1

为此,我认为最好的工具是array_map功能:

$followerPosts = array_map(function($f) {
    return $this->displayPostsAction($f->getFollower());    
}, $myFollowers['entities']);

var_dump($followerPosts);
于 2013-08-30T12:56:00.677 回答