简体   繁体   English

尝试比较两个数组中的值

[英]Attempting to compare values in two arrays

I have a list of users on a webpage and am trying to compare each username with a list of users in an array. 我在网页上有一个用户列表,正在尝试将每个用户名与数组中的用户列表进行比较。 But for some reason the following code always returns false. 但是由于某种原因,以下代码始终返回false。 Some usernames do match and should therefore display a yes next to the username. 某些用户名确实匹配,因此应在用户名旁边显示是。

foreach($result AS $user){
    foreach($listarray AS $name){
        if($user['username'] == $name){
            $whitelisted = 'Yes';
        } else {
            $whitelisted = 'No';
        }
    }
    echo '<tr><td><p>'.$user['username'].'</p></td><td><p>'.$user['location'].'</p></td><td><p>'.$user['date_joined'].'</p></td><td><p>'.$whitelisted.'</p></td>';
}

Why is this comparison returning false even when some names should match? 为什么即使某些名称应匹配,此比较也返回false?

You need to exit the loop with a break command when you find the match. 找到匹配项时,需要使用break命令退出循环。 Right now, your code is looping through each value in $result, then it takes that value and compares it to every value in $listarray. 现在,您的代码正在$ result中遍历每个值,然后将其取值并将其与$ listarray中的每个值进行比较。 It's not comparing side-by-side. 它不是并行比较。

You need to break the inner foreach loop if there is a matching element. 如果存在匹配的元素,则需要break内部的foreach循环。 Alternatively you can use the in_array function to check if there is value exists in an array 或者,您可以使用in_array函数检查数组中是否存在值

foreach($result AS $user){
    $whitelisted = 'No';
    if (in_array($user['username'], $listarray ))
    {
       $whitelisted = 'Yes';
    }
    echo '<tr><td><p>'.$user['username'].'</p></td><td><p>'.$user['location'].'</p></td><td><p>'.$user['date_joined'].'</p></td><td><p>'.$whitelisted.'</p></td>';
}

the result of the comparison is assigned to a simple variable, overwriting it each time. 比较结果分配给一个简单变量,每次都覆盖它。 that way if the last one is false it will always be false. 这样,如果最后一个为假,它将始终为假。 moreover, the second foreach will execute all its iteration before getting back to the first one. 此外,第二个foreach将执行所有迭代,然后返回第一个。 this can be fixed with continue; 可以通过continue; :

foreach($result AS $user){
  foreach($listarray AS $name){
    if($user['username'] == $name){
        $whitelisted = 'Yes';
        continue; // that way when conpared as true, your other foreach can display result
    } else {
        $whitelisted = 'No';
    }
}
echo '<tr><td><p>'.$user['username'].'</p></td><td><p>'.$user['location'].'</p></td><td><p>'.$user['date_joined'].'</p></td><td><p>'.$whitelisted.'</p></td>';
}
foreach($result AS $user){
    $whitelisted = 'No';
    foreach($listarray AS $name){
        if($user['username'] == $name){
            $whitelisted = 'Yes';
        }
    }
    echo '<tr><td><p>'.$user['username'].'</p></td><td><p>'.$user['location'].'</p></td><td><p>'.$user['date_joined'].'</p></td><td><p>'.$whitelisted.'</p></td>';
}
$a = array('me', 'you', 'ours');
$b = array('me', 'mine', 'you');

$merge = array_merge($a, $b); // MERGER ARRAY
$dups = array_count_values($merge); // COUNT DUPLICATES

// STORE ALL DUPLICATED VALUES
$dup = array();
foreach($dups as $k => $v) {
    if($v > 1) {
        $dup[] = $k;
    }
}

echo '<pre> Duplicates: '; print_r($dup); echo '</pre>';

Result: 结果:

 Duplicates: Array
(
    [0] => me
    [1] => you
)

Check out my PHP Fiddle: http://phpfiddle.org/main/code/t71-4db 查看我的PHP小提琴: http : //phpfiddle.org/main/code/t71-4db

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

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