简体   繁体   中英

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. 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?

You need to exit the loop with a break command when you find the match. Right now, your code is looping through each value in $result, then it takes that value and compares it to every value in $listarray. It's not comparing side-by-side.

You need to break the inner foreach loop if there is a matching element. Alternatively you can use the in_array function to check if there is value exists in an 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. this can be fixed with 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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