Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在函数中调用函数,直到满足条件_Php - Fatal编程技术网

Php 在函数中调用函数,直到满足条件

Php 在函数中调用函数,直到满足条件,php,Php,我正在查询salesforce,每次通话都会返回: 2000项记录 标记为“完成”的布尔值 要查询下一个2000条记录的下一个url 如果done为false,我需要对下一个url进行下一次调用,获取下一个2000条记录,检查done,并根据需要重复 $accounts = array(); function listAccounts($data,$accounts){ $ch = curl_init(); //curl options $result = curl_

我正在查询salesforce,每次通话都会返回: 2000项记录 标记为“完成”的布尔值 要查询下一个2000条记录的下一个url

如果done为false,我需要对下一个url进行下一次调用,获取下一个2000条记录,检查done,并根据需要重复

$accounts = array();

function listAccounts($data,$accounts){

    $ch = curl_init();
    //curl options
    $result = curl_exec($ch);
    curl_close($ch);
    $res = json_decode($result);
    foreach ($res->records as $k => $v){
            $accounts[] = $v->Name;
    }
    if ($res->done !== true){
            $data['listAccounts'] = $res->nextRecordsUrl;
            listAccounts($data,$accounts);
    }

    return $accounts;
}
print_r(listAccounts($data,$accounts));

上面,只返回第一个2000。我希望它重复listAccounts函数,直到$res->done!==符合事实的如果我在函数中回显$v->name,它将打印我期望的所有5000多条记录。我知道我需要将$accounts变量传递给下一个函数调用,但这似乎不行。

您可以使用
通过引用传递

 function listAccounts($data,&$accounts);
你的代码

$accounts = array();
function listAccounts($data,&$accounts){
  $ch = curl_init();
  //curl options
  $result = curl_exec($ch);
  curl_close($ch);
  $res = json_decode($result);
  foreach ($res->records as $k => $v){
          $accounts[] = $v->Name;
  }
  if ($res->done !== true){
    $data['listAccounts'] = $res->nextRecordsUrl;
    listAccounts($data,$accounts);
  }

return $accounts;
}
print_r(listAccounts($data,$accounts));