Omzy Posted December 21, 2008 Share Posted December 21, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/137909-solved-match-any-specified-items-in-an-array/ Share on other sites More sharing options...
harristweed Posted December 21, 2008 Share Posted December 21, 2008 http://us.php.net/manual/en/function.in-array.php Quote Link to comment https://forums.phpfreaks.com/topic/137909-solved-match-any-specified-items-in-an-array/#findComment-720786 Share on other sites More sharing options...
Mark Baker Posted December 21, 2008 Share Posted December 21, 2008 $newItems = array('item1', 'item2', 'item3'); $testArray = array_intersect($values, $newItems); Make with the result what you will Quote Link to comment https://forums.phpfreaks.com/topic/137909-solved-match-any-specified-items-in-an-array/#findComment-720794 Share on other sites More sharing options...
Omzy Posted December 21, 2008 Author Share Posted December 21, 2008 Cheers Mark. I did: if (array_intersect($values, $newItems)) I think that's working. Quote Link to comment https://forums.phpfreaks.com/topic/137909-solved-match-any-specified-items-in-an-array/#findComment-720810 Share on other sites More sharing options...
redarrow Posted December 21, 2008 Share Posted December 21, 2008 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/137909-solved-match-any-specified-items-in-an-array/#findComment-720823 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.