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?

Link to comment
Share on other sites

 

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.