2

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.

1

7 Answers 7

6

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

Comments

3
+200

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

Comments

0

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++)

Comments

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

Comments

0

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));

3 Comments

Actually i want to use strpos for comparing string values
What exactly are you trying to do ?
I want to use this if statement if(strpos($parts1[$j],$parts2[$i]) !== false) i cant use in_array function
0

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

Comments

0

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
)

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.