简体   繁体   English

如何比较从两个不同的提取数组中检索到的值

[英]how to compare values retrieved from two different fetch array

I have two fetch arrays retrieved from the database, and try to compare two values until they have the matching, but it seems bit hard to figure out where I got wrong. 我有两个从数据库中检索到的提取数组,并尝试比较两个值,直到它们匹配为止,但是似乎很难弄清楚我哪里写错了。

the two database have upto 2 jobrequestnumber so, the number of the matching is supposedly two, but it only counts one(indicated below). 这两个数据库最多有2个jobrequestnumber,因此,匹配的数目据说是2,但是只计一个(如下所示)。

 $value1=array();
 $value2 =array();

 $queryfordispatch = "select jobrequestnumber from dispatch";
 $resultfordispatch = mysql_query($queryfordispatch);
 $valuefordispatch =mysql_fetch_array($resultfordispatch);
 $value1 = $valuefordispatch['jobrequestnumber'];

 $queryforjobrequest = "select jobrequestnumber from jobrequest";
 $resultforjobrequest = mysql_query($queryfordispatch);
 $valueforjobrequest =mysql_fetch_array($resultforjobrequest);
 $value2 = $valueforjobrequest['jobrequestnumber'];

 $cfd=count($valuefordispatch);
 $cfj=count($valueforjobrequest);

//In this for loop, if I try to echo the value of $value1, it only produce the value of "1" //在此for循环中,如果我尝试回显$ value1的值,则只会产生值“ 1”

for($i=1; $i<=$cfd; $i++){
for($j=1; $j<=$cfj; $j++){
if ($value1 == $value2 ){

    $queryforupdate ="UPDATE jobrequest SET status = 'processed' where jobrequestnumber = $value2 ";
    mysql_query($queryforupdate);
  }
 }
}

So the result that I expect is that 所以我期望的结果是

jobnum Jobnum

 1=1? yes --> update
 1=2? no  --> discard
 2=1? no  --> discard
 2=2? yes --> update

If you 're trying to see if array1 and array2 share values then use something like this. 如果要查看array1和array2是否共享值,请使用类似以下的内容。

$a = array('1','2','3','4','5','6','7','8','9');
$b = array('A','B','C','4','5','6','7','E','F');
$num_shared = 0;

foreach($a as $key => $val) {
    if (in_array($val, $b)) {
        echo $val . '<br />';
        $num_shared++;
    }
}

echo '<strong>'. $num_shared . '</strong>';

Let's start with this: 让我们从这个开始:

Welcome to Stack Overflow! 欢迎使用Stack Overflow! Please, don't use mysql_* functions in new code . 请不要在新代码中使用mysql_*函数 They are no longer maintained and are officially deprecated . 它们不再维护,已正式弃用 See the red box ? 看到红色框了吗? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. 相反,要了解准备好的语句 ,并使用PDOMySQLi- 本文将帮助您确定哪一个。 If you choose PDO, here is a good tutorial . 如果您选择PDO, 这是一个很好的教程

As for finding the difference between two arrays, one can always use array_diff : 至于找到两个数组之间的差异 ,可以始终使用array_diff

 <?php //Example #1 array_diff() example $array1 = array("a" => "green", "red", "blue", "red"); $array2 = array("b" => "green", "yellow", "red"); $result = array_diff($array1, $array2); print_r($result); ?> 

Multiple occurrences in $array1 are all treated the same way. $ array1中的多次出现都以相同的方式处理。 This will output: 这将输出:

 Array ( [1] => blue ) 

However, to compare each value separately, you need to loop through both arrays at the same time (with a for loop, preferably), and compare each and every item. 但是,要分别比较每个值,您需要同时遍历两个数组(最好使用for循环),并比较每个项目。 Which is kind of what you're already doing. 这是您已经在做的事情。

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

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