简体   繁体   English

在PHP中从嵌套的foreach中删除数组值

[英]Removing an array value from nested foreach in PHP

I am essentially trying to compare Array1, with Array2, to find matching rows. 我本质上是试图将Array1与Array2进行比较,以找到匹配的行。 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. 但是,两个数组都有大约14000行(来自sql表),因此我认为逻辑上删除一旦找到array2中的匹配行,以减少总体迭代次数,将是合乎逻辑的。

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) 注意:数组1和2的数据来自两个单独的数据库,并且我无法一次在两个数据库上运行查询(因此必须在php中执行此操作)

It appears that code will unset $array2 itself, and the local copy of the row within your loop ($array2Row). 看来代码将取消设置$ array2本身,并取消循环中该行的本地副本($ 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]);
        }
    }
}

There is missing of ")" into if condition. if条件中缺少“)”。 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);
      }
  }

} }

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]);
  }
}

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() . 如果需要在使用SQL的情况下查找匹配的行,只需将结果放入ID为键的关联数组中,然后使用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: 这应该是最快的方法,并且由于每个数组中都有〜14K条目-我将采用以下解决方案:

$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);

you should make use of php's $key => $value args of the foreach function, so the code would be something like this: 您应该使用foreach函数的php的$key => $value args,因此代码如下所示:

$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]);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM