Home > front end >  How to stop second foreach from looping more than once
How to stop second foreach from looping more than once

Time:06-28

I have an query which select all ids from a table. Once I have all id's, they are stored in an array which I foreach over.

Then there is an second array which pull data from url (around 5k rows) and should update DB based on the id's.

The problem - second foreach is looping once for each ID, which is not what I want. What I want is to loop once for all id's.

Here is the code I have so far

$query = "  SELECT id, code FROM `countries` WHERE type = 1";
$result = $DB->query($query);

$url = "https://api.gov/v2/data?api_key=xxxxx";
    $api_responce = file_get_contents($url);
    $api_responce = json_decode($api_responce);
    $data_array = $api_responce->data;
    
$rows = Array();
while($row = $DB->fetch_object($result)) $rows[] = $row;

foreach ($rows as $row) {
 
    foreach ($data_array as $key => $dataArr) {
    
        $query = "UPDATE table SET data_field = $dataArr->value WHERE country_id = $row->id LIMIT 1";
    
    }
}

The query returns 200 id's and because of than the second foreach (foreach ($data_array as $key => $dataArr) { ... }) execute everything 200 times.

It must execute once for all 200 id's not 200 * 5000 times.

CodePudding user response:

Since the question is aboot using a loop, we will talk about the loop, instead of trying to find another way. Actually, I see no reason to find another way.

->Loops and recursions are great, powerful tools. As usually, with great tools, you need to also find ways of controlling them. See cars for example, they have breaks. The solution is not to be slow and sit to horses era, but to have good brakes.

->In the same spirit, all you need to master the power called recursions and loops is to stop them properly. You can use if cases and "break" command in PHP.

For example, here we have a case of arrays containing arrays, each first child of the array having the last of the other (1,2,3), (3,4,5) and we want to controll the loop in a way of showing data in a proper way (1,2,3,4,5). We will use an if case and a counter :

<?php
$array = array( array(-1,0,1), array(1,2,3,4,5), array(5,6,7,8,9,10), array(10,11,12,13,14,15) );

static $key_counter;
foreach( $array as $key ){
    $key_counter = 0;
    foreach( $key as $key2 ){
        if ( $key_counter != 0 ) {
            echo $key2 . ', ';
        }
        $key_counter = $key_counter   1;
    }
}

Since I dont have access to your DB is actually hard for me to run and debbug the code, so the best I can say is that you need to use an if case which checks if the ID of the object is the ID we want to proccess, then proceed to proccessing.


P.S. Static variables are usefull for loops and specially for recurrsions, since they dont get deleted from the memory once the functions execution ends.

The static keyword is also used to declare variables in a function which keep their value after the function has ended.

  •  Tags:  
  • php
  • Related