dongmeng4742 2018-08-15 19:28
浏览 39

PHP CLI -f数组联合在5000个元素后停止

I'm trying to parse an API response that can have thousands of results so it provides a @odata.nextLink. I want to return all of the results in an array so after I make a curl request that has a nextLink, I use php aray union to add to the previous array with the result

$result_array = $api_response['value']; //store initial response

while (array_key_exists('@odata.nextLink', $api_response)) {

    curl_setopt_array($curl, array(
       CURLOPT_URL => $api_response['@odata.nextLink'],
       (more options)...
    )):

    $response = curl_exec($curl);
    $err = curl_error($curl);

    curl_close($curl);

    if ($err) {
        echo "cURL Error #:" . $err;
    } else {
        echo $response;
        $api_response = json_decode($response, TRUE);
        $result_array = $result_array + $api_response['value'];
    }
}

A response looks like this

{
  "@odata.context":"https:/api.com/v1/query","value":[
    {
      "Sku":"ABC","ID":123
    },
    (more records)...
  ],"@odata.nextLink":"https://api.com/v1/query&$skip=200"
}

array looks like this with print_r

(
    [0] => Array
        (
            [Sku] => abc
            [ID] => 123
        )

    [1] => Array
        (
            [Sku] => efg
            [ID] => 456
        )
    (more results)...

)

However, it stops adding elements to the array after 5000. I execute the script file from php -f for testing. I try ini_set('memory_limit', '1000M'); like suggested to overcome memory limit but it doesn't help. Is this some sort of limitation for the PHP CLI? On a live server would this still happen? Eventually I'd like to make another curl request on each product id in a large data set (probably in a batch) to see if the api can handle it.

Edit: When I meant set up curl, I was changing the curl request to next link. I added the change for clarification and show that it doesn't loop infinitely.

Edit 2: OK so I have a better deal of what's happening. The API returns the value as a 2d array, each inner array is a different record. It returns 100 results a few times until it serves 5000. The array key resets each time. The after setting the initial $result_array is set to first 100 results, the plus operator doesn't append the next 100 since they are the same keys. Once API starts returning 5000 results, it appends from index 100 to 4999. Subsequent requests don't add more like previously. Is there a an array function in php that can append multidimensional arrays to each other like + does with one dimensional arrays? I could do a foreach to append properly but it seems tedious if there's already a built-in method.

  • 写回答

2条回答 默认 最新

  • dongqiaolong9034 2018-08-15 19:54
    关注

    one thing to note about using + with arrays is if the elements keys are the same or the array is not explicitly keyed, no elements will be added.

    for example:

    var_dump(['a','b','c'] + ['d','e','f']); 
    /* outputs
    array(3) {
      [0]=> "a"
      [1]=> "b"
      [2]=> "c"
    }
    */
    
    print_r([0=>'a',1=>'b',2=>'c'] + [3=>'d',4=>'e',5=>'f']);
    /* outputs
    array(6) {
      [0]=>"a"
      [1]=>"b"
      [2]=>"c"
      [3]=>"d"
      [4]=>"e"
      [5]=>"f"
    } */
    

    if you want to add elements with same keys, use array_merge

    print_r(array_merge(['a','b','c'], ['d','e','f']));
    /* outputs
    Array
    (
        [0] => a
        [1] => b
        [2] => c
        [3] => d
        [4] => e
        [5] => f
    )
    */
    
    评论

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备