Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mvnobrega's avatar

Best way to restart foreach

I have an API that provides me with historical cryptocurrency quote data. However, it provides me with data every 4 years, so I have to repeat the request until I finish the entire history of a currency. Currently I'm doing it like this:

public function intraday(){

            $client = new Client();

            $indices = IndiceCriptomoedas::all();

            $dataInicial = '2013-11-01';
            $dataFinal = '2017-11-01';


            foreach($indices as $symbol){

                $response = $client->request('GET', 'https://financialmodelingprep.com/api/v3/historical-chart/1day/'.$symbol->indice.'?from='.$dataInicial.'&to='.$dataFinal.'&apikey=KAKAKKAKKA');

                $moedas = json_decode($response->getBody());

                foreach ($moedas as $moeda) {

                        $novo = new CriptomoedasIntraday();
                        $novo->symbol = $symbol->indice;
                        $novo->close = $moeda->close;
                        $novo->close_price = number_format($moeda->close, 2, ',', '.');
                        $novo->volume = $moeda->volume;
                        $novo->date = $moeda->date;
                        $novo->save();

                        $dataInicial = date('Y-m-d', strtotime($dataInicial . ' +4 years'));
                        $dataFinal = date('Y-m-d', strtotime($dataFinal . ' +4 years'));
                        $symbol->intraday_enviado = true;
                        $symbol->update();
                
                }
            }
        }

But it happens that after saving the first batch to the database, I call the code to include another 4 years in the start and end date, but these 4 years are included in the next currency loop. How do I do it and what would be the best way for me to go through the entire history of a currency, and then go to the next one?

0 likes
5 replies
Snapey's avatar

but these 4 years are included in the next currency loop.

I have no idea what this means

Snapey's avatar

because you add 4 years to the dates instead of just moving to next symbol

mvnobrega's avatar

@Snapey each "symbol" can have price history ranging from "2013-11-01" to the current date. And the API allows me to grab 4 years at a time.

So I need to go through this entire range in each cryptocurrency. In other words, in the Dogecoin currency, for example, (with DOGEUSD symbol) I need to get the entire history "2013-11-01" until today. But since the API allows me to take 4 years at a time, I need to loop through all the years in that cryptocurrency before moving on to the next one.

And so on

Snapey's avatar
Snapey
Best Answer
Level 122

@mvnobrega either way, you need two nested loops, one for the years and one for the symbol.

You only have one loop at present, and also no way of knowing when to stop.

public function intraday()
{
    $indices = IndiceCriptomoedas::all();
    $client = new Client();
    foreach(range(2013, today()->year) as $year) {
       
        foreach($indices as $symbol) {

                $response = $client->request('GET', 'https://financialmodelingprep.com/api/v3/historical-chart/1day/'.$symbol->indice.'?from=' . $year . '11-01&to=' . $year+1 . '11-01&apikey=KAKAKKAKKA');

                $moedas = json_decode($response->getBody());

                foreach ($moedas as $moeda) {

                        $novo = new CriptomoedasIntraday();
                        $novo->symbol = $symbol->indice;
                        $novo->close = $moeda->close;
                        $novo->close_price = number_format($moeda->close, 2, ',', '.');
                        $novo->volume = $moeda->volume;
                        $novo->date = $moeda->date;
                        $novo->save();
                        $symbol->intraday_enviado = true;
                        $symbol->update();
                
                }
            }
        }
2 likes

Please or to participate in this conversation.