Andy
Andy

Reputation: 153

How to check if results from one array are in another array and display the one that isnt?

I have 2 arrays that fetches usernames in a loop. At the end of the loop there should be 1 user that is only in one array, all other users will be in both. I want the output to display the username of the user that is only listed in one of the arrays.

End array example as follows:

$usrArray = Array ( [0] => Dave[1] => Andy[2] => Mike);
$looseArray = Array ( [0] => Dave[1] => Mike);

I want an output to display just "Andy" as he is the only user listed in only 1 array.

Appreciate the help. Thanks.

Upvotes: 0

Views: 34

Answers (2)

faizan siddiqui
faizan siddiqui

Reputation: 84

Like this...

$usrArray = array('Dave','Andy','Mike');
$looseArray = array('Dave','Mike');

 foreach ($usrArray as $key => $value) {
    if(!in_array($value, $looseArray)){
        echo $value;
    }
 }

Upvotes: 1

wxker
wxker

Reputation: 1896

https://www.php.net/manual/en/function.array-diff.php

array_diff($usrArray, $looseArray)

Upvotes: 0

Related Questions