doumeng2637 2017-09-05 05:09
浏览 33
已采纳

PHP按值调用返回较少的值到main函数

I have a weird issue, i wrote an recursive function to get more results from facebook. In the called function(recursive) i am returning the values to the main function. In the called function when i print the returned value it displays the exact value(generally the array size which is 90). But in the main function when i print the returned value, it is always less(the array size is exactly 50 each time). Here is my code..

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;
    }
}

What is the issue with my code..?

  • 写回答

1条回答 默认 最新

  • duanguoping2016 2017-09-05 05:16
    关注

    You are using a recursive function but not used the value return by the inner call...

    The fixed code is:

    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;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图