Search code examples
phpif-statementforeachstrpos

Show only ones that do not match PHP


I'm trying to use a multiple foreach and if statements to give me a list of list of people that have not been matched. I have the below code, I am able to get this to successfully give me a list of people it does match.

What I want to do is it echo each the ID from the $tenant_id foreach that have not been found in the $value2 foreach, am I doing something wrong? It will only output nothing?

foreach($array_93 as $value) {
    $tenant_id = $value['id'];
    $limit = 0;
    foreach($obj->response->entries as $value2) {
        if($limit==1) break;
        if ($value2->{100} == 'true' && $value2->{114} == $tenant_id) 
            {echo $value['id']; // This should echo ID's that have not been found.}
            $limit++;
        }
    }
};

UPDATE >>

After continuing to try and get this working I have got to this point, I am able to to use this to show which ID's are all 'n' as per screenshot after. The first one is all n's so this has not matched, how can I now make just the ones with all n's ID show?

foreach($array_93 as $value) {
echo '<b>'.$value['id'].'</b>';
echo '<br />';

foreach($obj->response->entries as $value2) {
    if (strpos($value2->{114}, $value['id']) === false) 
        {
        echo '<i>n</i>';
    } else {
        echo '<b>Y</b>';
    }  
}
echo '<br />';
};

enter image description here


Solution

  • Use a flag with Y-found state:

    foreach($array_93 as $value) {
        $Yfound = false;
    
        foreach($obj->response->entries as $value2) {
            if (strpos($value2->{114}, $value['id']) !== false) {
                $Yfound = true;
            }
        }
        if(!$Yfound) {
            echo $value['id'] . ' has n`s only<br>';
        }
    }