0

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?

3
  • 1
    Please post some sample data; hard to tell what's wrong without seeing your data structure. Commented Dec 6, 2013 at 18:51
  • Does $user['username'] exist? Do the cases match? Is there extra whitespace in either $user['username'] or $name? You'll probably need to post some actual data to get a good answer here :) Commented Dec 6, 2013 at 19:00
  • Yes it does exist and no there is ño white space Commented Dec 6, 2013 at 19:30

5 Answers 5

1

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>';
}
Sign up to request clarification or add additional context in comments.

1 Comment

More elegant thank you, I totally forgot the in_array function.
1

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.

Comments

1

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>';
}

Comments

0
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>';
}

3 Comments

You need to break the inner foreach if there is a matching element. Assume if first element is the match but last element is not a match then $whitelisted will always be NO
Khawer, that is wrong: "No" will only be seen, if there is no hit - it is assigned before the inner loop
Sorry my bad got it now, But still breaking the inner loop will save some time as if there is a match then there is no need to compare any further.
0
$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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.