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条)

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记