Jump to content

[SOLVED] Match any specified items in an array


Omzy

Recommended Posts

Lets say I have an array called $values

 

I use a series of IF statements to add items to the array using array_push

 

I now want to use an IF statement to check if ANY of the possible items exist in the array.

 

Normally I'd do something like:

 

if (in_array('item1', $values) || in_array('item2', $values) || in_array('item3', $values))

 

But is there another (simpler) way of doing this? Can I make the list of all possible items and put that in an array or something?

 

two array functions that might help.

 

Show the matches. echo them out.

<?php
$array1 = array("a" => "green", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
$result = array_intersect($array1, $array2);
print_r($result);
?>

 

 

 

 

show the elements that are not matched

<?php
$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);

print_r($result);
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.