2017-09-05 76 views
0

我有一个奇怪的问题,我写了一个递归函数来从Facebook获得更多的结果。在被调用函数(递归)中,我将值返回给主函数。在被调用的函数中,当我打印返回的值时,它会显示确切的值(通常是90的数组大小)。但是在我打印返回值的主函数中,它总是少一些(每次数组大小恰好为50)。这是我的代码..PHP调用通过值返回较少的值到主函数

public function mainFunction(){ 
    $response = $fb->get('/me?fields=id,name,accounts', $useraccesstoken);  
    $userData = $response->getDecodedBody(); 
    $pages = $userData['accounts']; 
    $pages = $this->getMorePages($pages); 
} 

public function getMorePages($pages){ 
    if(count($pages)>1 && isset($pages['paging']['next'])){ 
     $morePages = file_get_contents($pages['paging']['next']); 
     $morePages = json_decode($morePages,true); 
     foreach($morePages['data'] as $page){ 
      array_push($pages['data'],$page); 
     } 
     if(count($morePages)>1 && isset($morePages['paging']['next'])) { 
      $pages['paging']['next']=$morePages['paging']['next']; 
      $this->getMorePages($pages); 
     } 
     return $pages; 
    }else{ 
     return $pages; 
    } 
} 

我的代码有什么问题..?

回答

1

您正在使用一个递归函数,但不使用内调用的值返回...

固定的代码是:用于相同的目的

public function getMorePages($pages){ 
    if(count($pages)>1 && isset($pages['paging']['next'])){ 
     $morePages = file_get_contents($pages['paging']['next']); 
     $morePages = json_decode($morePages,true); 
     foreach($morePages['data'] as $page){ 
      array_push($pages['data'],$page); 
     } 
     if(count($morePages)>1 && isset($morePages['paging']['next'])) { 
      $pages['paging']['next']=$morePages['paging']['next']; 

      // Add return values to the main array 
      //$pages += $this->getMorePages($pages); 
      // For more support use array_merge function 
      $pages = array_merge($this->getMorePages($pages), $pages) 
     } 
     return $pages; 
    }else{ 
     return $pages; 
    } 
} 
+0

上述array_push()方法array_merge()做到了。 –

+0

@RAUSHANKUMAR,但你不会推到数组递归数组返回... –

+0

好吧,我会测试你的代码 –