douba4933 2018-08-08 07:39
浏览 54
已采纳

递归cURL函数php

I am having a problem with a php function, it is a recursive function - which seems to work, because if i dump the array I see the items are added to the array. But when I want to return the array I get a null.

The api function is a http curl wrapper function which handles the api call - that works fine, also the $arr get's filled with the db_address items - but the return doesn't return the array - if I var_dump the result I get a Null, if I print_r the result I just get a 1.

function get_all_db_addresses($arr, $skip){
if(!isset($arr)){
    $arr = [];
}
if(!isset($skip)){
    $skip = 0;
}
$db_addresses = json_decode(db_api('GET', 'DbAddress?$skip=' . $skip), true);
$arr = array_merge($arr, $db_addresses['value']);
if(!array_key_exists('@odata.nextLink', $db_addresses)){
    return $arr;
} else {
    $skip += 20;
    get_all_db_addresses($arr, $skip);
}
}

Am i doing something wrong here? I don't see what i'm doing wrong here..

  • 写回答

4条回答 默认 最新

  • dongwu8050 2018-08-08 08:02
    关注

    You have 2 problems:

    1. You don't have return statement when performing recursive call
    2. In case you perform array_merge with null, you will get null

    Check this:

    function get_all_db_addresses($arr = array(), $skip = 0)
    {
        $db_addresses = json_decode(db_api('GET', 'DbAddress?$skip=' . $skip), true);
        if (isset($db_addresses['value'])) {
            $arr = array_merge($arr, $db_addresses['value']);
        }
        if (array_key_exists('@odata.nextLink', $db_addresses)) {
            return get_all_db_addresses($arr, $skip + 20);
        }
        return $arr;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改