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 用matlab写代码
  • ¥30 motoradmin系统的多对多配置
  • ¥15 求组态王串口自定义通信配置方法或代码?
  • ¥15 实验 :UML2.0 结构建模
  • ¥20 用vivado写数字逻辑实验报告撰写,FPGA实验
  • ¥15 为什么shp文件会有这种小方块?
  • ¥15 ecplise在连接数据库时显示加载驱动成功但是数据库连接失败
  • ¥15 visionmaster启动失败,提示为“机器不满足授权而被禁用”
  • ¥15 IDEA中圈复杂度如何具体设置
  • ¥50 labview采集不了数据