I have a recursive function in PHP where it loops in an api where it only allows you to retrieve 200 records of it at a time.
But as this API has a very high latency response we decided to use a local intermediate database where I add these records and it will be shown on the site.
But since this API has more than 30 thousand records, the recursive function consumes a lot of memory because in the case of 30 thousand records it would have to be invoked recursively over 1500 times and this ends up giving the famous StackOverflow.
I would like to know if there is a manual way of clearing the memory of this function by calling it again without losing its value.
Code sample:
public function recursive($index = 0, $offset = 200){
$api = getConectApi($index,offset)->getRecords();
foreach($api as $value){
//aqui ocorre meu loop necessário
}
if(count($api) > 0){
$this->recursive($index+200,$offset+200);
}
}
I would like to find a way that when I call the recursive function again delete the previous allocation without losing the reference value that was passed.