Shuma
Shuma

Reputation: 293

Removing an array value from nested foreach in PHP

I am essentially trying to compare Array1, with Array2, to find matching rows. However, both arrays have around 14000 rows (from sql table), so I figured it would be logical to delete matching rows in array2 once found, to reduce the number of iterations overall.

It looks something like this:

foreach($array1 as $arrayRow){
    foreach($array2 as $array2Row){
        if($arrayRow['ID'] == $array2Row['ID']{
         $matchfound = 1;
         unset($array2,$array2Row);
        }
    }
}

However seemingly, nothing happens at all when running the above code.

Note: The data for array 1 and 2 come from two separate databases, and I am unable to run a query on both at once (hence having to do this in php)

Upvotes: 0

Views: 154

Answers (5)

James Waddington
James Waddington

Reputation: 2905

It appears that code will unset $array2 itself, and the local copy of the row within your loop ($array2Row). Instead, get the key for the row you want to unset, and unset the entry directly:

foreach($array1 as $arrayRow){
    foreach($array2 as $key => $array2Row){
        if($arrayRow['ID'] == $array2Row['ID']{
           $matchfound = 1;
           unset($array2[$key]);
        }
    }
}

Upvotes: 2

Achmad Mahardi
Achmad Mahardi

Reputation: 81

you should make use of php's $key => $value args of the foreach function, so the code would be something like this:

$matchFound = 0;
foreach($array1 as $arrKey1 => $arrVal1) {
    foreach($array2 as $arrKey2 => $arrVal2) {
        if ($array1[$arrKey1]['ID'] == $array2[$arrKey2]['ID']) {
            $matchFound = $matchFound + 1;
            unset($array2[$arrVal2]);
        }
    }
}

Upvotes: 0

Kleskowy
Kleskowy

Reputation: 2668

If you need to find matching rows without using SQL, just put the results in associative arrays with ID as key, and then use array_instersect_key().

This should be the fastest way to do it, and since you have ~14K entries in each array - I'd go with the solution below:

$array1 = $array2 = array();
//I assume $query1Result and $query2Result are results of sql queries from 2 databases

//put rows in arrays with ID as key
while ($row = mysqli_fetch_assoc($query1Result)) {
   $array1[$row['ID']] = $row; // ID is the key in array
}

while ($row = mysqli_fetch_assoc($query2Result)) {
   $array2[$row['ID']] = $row; // ID is the key in array
}

//then use this to compute the intersection
$intersected = array_intersect_key($array1, $array2);

Upvotes: 0

art-fan-vikram
art-fan-vikram

Reputation: 315

Check this solution to unset matching array element

 //Array1 is $array1
//Array2 is $array2 
foreach($array1 as $key => $value){
 if(in_array($value,$array2)){
    unset($array2[$key]);
  }
}

Upvotes: 0

Ghanshyam Dekavadiya
Ghanshyam Dekavadiya

Reputation: 153

There is missing of ")" into if condition. You can run this code it is working.

foreach($array1 as $arrayRow){
    foreach($array2 as $array2Row){
       if($arrayRow['ID'] == $array2Row['ID']){
       $matchfound = 1;
       unset($array2,$array2Row);
      }
  }

}

Upvotes: 1

Related Questions