2017-09-05 78 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

好吧,我會測試你的代碼 –