0

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..

Jouby
  • 2,196
  • 2
  • 21
  • 33
Ernst Reidinga
  • 203
  • 2
  • 13
  • You are not returning the result of the call in the else branch, so it just vanishes into thin air. See https://softwareengineering.stackexchange.com/questions/201765/reason-for-return-statement-in-recursive-function-call for a more detailed explanation. – CBroe Aug 08 '18 at 07:50

4 Answers4

0

You need to create global variable and merge next array to it

function get_all_db_addresses($skip)
{
    if(!isset($skip)){
        $skip = 0;
    }
    $db_addresses = json_decode(db_api('GET', 'DbAddress?$skip=' . $skip), true);

    $arr = array();
    if(!array_key_exists('@odata.nextLink', $db_addresses)){
        $arr = array_merge($arr, $db_addresses['value']);
    } else {
        $skip += 20;
        $arr = array_merge($db_addresses['value'], get_all_db_addresses($skip));
    }
    return $arr;

}

Not sure, but possiblly it works as I can't test your data

Jaydp
  • 1,029
  • 9
  • 17
  • Thank you but it is not working, that said - when i var_dump the array after the: if(!array_key_exists('@odata.nextLink', $db_addresses)){ then i can see the array is filled.. – Ernst Reidinga Aug 08 '18 at 08:00
0

Try this:

function get_all_db_addresses($arr = [], $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;
        return get_all_db_addresses($arr, $skip);
    }
}

You have to add return statment for recursive call. And tip: don't check if parameter isset - set for it default value.

JoelP
  • 19
  • 2
0

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;
}
Dima L.
  • 3,443
  • 33
  • 30
0

Problems in your code:

  1. Your function argument should be passed reference if you want to change the value of argument References Explained.
  2. You should always return a value (with specified type) if you expect the function to.
  3. Default argument value can be set directly in new PHP versions, but this should not break your code, just coding style.

Solution:

Your function should be modified to this to work correctly (haven't tested):

function &get_all_db_addresses(&$arr = [], $skip = 0) {
    // ...
    if(array_key_exists('@odata.nextLink', $db_addresses)){
        get_all_db_addresses($arr, $skip + 20);
    }
    return $arr;
}
alphakevin
  • 515
  • 3
  • 11