h_h
h_h

Reputation: 1231

PHP array comparison and finding matched and non matched items

I have been using this script for finding matched and nonmatched array items.

My code is.

$filter1 = "red,green,blue,yellow";         
$parts1 = explode(',', $filter1);

$filter2 = "red,green,blue";        
$parts2 = explode(',', $filter2);

for($i=0; $i< count($parts1); $i++)
{

    for($j=0; $j< count($parts2); $j++)
    {

        if($parts1[$i] == $parts2[$j])
        {
            $match[] = $parts1[$i];
        } else {
            $nomatch[] = $parts1[$i];
        }
    }
}

print_r($match);
echo "<br>";
print_r($nomatch);

By using this code i am only getting the matched items and not nonmatched. Can anybody help. Thanks in advance.

Upvotes: 2

Views: 2178

Answers (7)

Faisal
Faisal

Reputation: 4765

You can find matching value using array_intersect and non matching value using array_diff.

Here you can see LIVE DEMO

/**
 * @param $arr
 */
function pr($arr)
{
    echo '<pre>';
    print_r($arr);
    echo '</pre>';
}

$filter1 = "red,green,blue,yellow";         
$parts1 = explode(',', $filter1);

$filter2 = "red,green,blue";        
$parts2 = explode(',', $filter2);

$match = array_intersect($parts1, $parts2);
$nomatch = array_diff($parts1, $parts2);
pr($match);
pr($nomatch);

Output Screen:

Array
(
    [0] => red
    [1] => green
    [2] => blue
)
Array
(
    [3] => yellow
)

Upvotes: 0

air4x
air4x

Reputation: 5683

Try the below code

$filter1 = "red,green,blue,yellow";
$parts1 = explode(',', $filter1);

$filter2 = "red,green,blue,purple";
$parts2 = explode(',', $filter2);

$matching = array_intersect($parts1, $parts2);
$non_matching = array_diff(array_merge($parts1, $parts2), $matching);

Changing your code, which should have similar result for non-matching as array_diff($parts1, $parts2);

for($i=0; $i< count($parts1); $i++)
{
  $is_matching = false;
  for($j=0; $j< count($parts2); $j++)
  {
    if($parts1[$i] == $parts2[$j])
    {
      $is_matching = true;
      break;
    }
  }
  if ($is_matching) {
    $match[] = $parts1[$i];
  } else {
    $nomatch[] = $parts1[$i];
  }
}

Upvotes: 0

Shahrokhian
Shahrokhian

Reputation: 1114

try this

$filter1 = "red,green,blue,yellow";         
$parts1 = explode(',', $filter1);

$filter2 = "red,green,blue";        
$parts2 = explode(',', $filter2);

foreach($parts1 as $first)
{
    if(in_array($first, $parts2))
    {
        $match[] = $first;
    }
    else 
    {
        $nomatch[] = $first;
    }
}

print_r($match);
echo "<br>";
print_r($nomatch);

or you can use array_diff to get non matched items

print_r(array_diff($parts1,$parts2));

and for matched items use

print_r(array_intersect($parts1,$parts2));

Upvotes: 0

Lucio Paoletti
Lucio Paoletti

Reputation: 361

$filter1 = "red,green,blue,yellow";         
$parts1 = explode(',', $filter1);

$filter2 = "red,green,blue";    
$parts2 = explode(',', $filter2);   

$match = array();
$nomatch = array();

foreach($parts1 as $v){     
    if(in_array($v,$parts2))
        $match[]=$v;
    else
        $nomatch[]=$v;
}

Upvotes: 0

StaticVariable
StaticVariable

Reputation: 5283

this can be done by array_intersect and array_diff

$filter1 = "red,green,blue,yellow";         
$parts1 = explode(',', $filter1);

$filter2 = "red,green,blue";        
$parts2 = explode(',', $filter2);


$result = array_intersect($parts1 , $parts2 );
print_r($result);

Live Example

enter image description here

and

$result = array_diff($parts1 , $parts2 );

print_r($result);

LIVE example

enter image description here

Upvotes: 3

Man Programmer
Man Programmer

Reputation: 5356

because your nested loop not run at yellow color time try this

$filter1 = "red,green,blue,yellow";    
$filter2 = "red,green,blue,gray";

or

for($j=0; $j<= count($parts2); $j++)

Upvotes: 0

Baba
Baba

Reputation: 95161

You can try using array_intersect and array_diff

$filter1 = "red,green,blue,yellow";         
$parts1 = explode(',', $filter1);

$filter2 = "red,green,blue";        
$parts2 = explode(',', $filter2);

$match = array_intersect($parts1, $parts2);
$nomatch = array_diff($parts1, $parts2);

var_dump($match,$nomatch);

Output

array
  0 => string 'red' (length=3)
  1 => string 'green' (length=5)
  2 => string 'blue' (length=4)
array
  3 => string 'yellow' (length=6)

Upvotes: 6

Related Questions