vineld Posted July 9, 2009 Share Posted July 9, 2009 Which method is the most efficient if you wish to find out if a number of values all exist within an array? Quote Link to comment Share on other sites More sharing options...
Philip Posted July 9, 2009 Share Posted July 9, 2009 in_array perhaps? Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 9, 2009 Share Posted July 9, 2009 Well, when you say "if a number of values all exist within an array" the best answer may be array_diff(), array_diff_assoc(), array_intersect() or array_intersect_assoc(). Your question was a little too vague. Perhaps you could provide a real example of what you are wanting to accomplish. Quote Link to comment Share on other sites More sharing options...
vineld Posted July 11, 2009 Author Share Posted July 11, 2009 Maybe I should have made the question a little bit more specific although I will instead make it wider =) More specifically I am thinking about these two scenarios: Array example values: 5, 6, 8, 12, 16, 23, 35 Scenario 1: Finding out if either 5, 8 or 23 exist in the array. Scenario 2: Finding out if both 5, 8 and 23 exist in the array. In the second case array_diff is definitely the best choice but what about the first? Same and count the result? If the arrays are large this might not be a good idea? In_array is the obvious choice for a single value but I'm not entirely sure of the functionality of in_array when the needle is an array? Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 11, 2009 Share Posted July 11, 2009 For scenario 1 I would use array_intersect() and see if the resulting array count is > 0. If so, you know one or more of the values (5,8,23) exist in the target array. Quote Link to comment Share on other sites More sharing options...
Philip Posted July 11, 2009 Share Posted July 11, 2009 I'd agree with mjdamato on this one. Code examples below: <?php $a = array(5, 6, 8, 12, 16, 23, 35); $b = array(5, 8, 23); //$b = array(5, 8, 23,55); // Shows what happens when a number isnt in $a // To check to see if some of the values are in the array (Scenario 1): if(count(array_intersect($b, $a))>0) { echo count(array_intersect($b, $a)),' elements were from array(5, 8, 23) were in array(5, 6, 8, 12, 16, 23, 35)<br>'; } // To check to see if all of the values are in the array (Scenario 2): if(!count(array_diff($b,$a))) { echo 'All elements from array(5, 8, 23) were in array(5, 6, 8, 12, 16, 23, 35)<br>'; } else { echo 'You were missing the following: '; print_r(array_diff($b,$a)); } ?> Quote Link to comment Share on other sites More sharing options...
Q695 Posted July 11, 2009 Share Posted July 11, 2009 Actually I dealt with this problem a few years ago, and it was solved with a "%$value%". I love my "SQL in easy steps" book that a prof reccomended to me several years ago . Quote Link to comment Share on other sites More sharing options...
Philip Posted July 11, 2009 Share Posted July 11, 2009 Actually I dealt with this problem a few years ago, and it was solved with a "%$value%". Uhh.. Quote Link to comment Share on other sites More sharing options...
Q695 Posted July 11, 2009 Share Posted July 11, 2009 I meant months, and that's how you do a search for strings, and you can do a loop back for strings within strings. What did you mean by "uhh.."? Quote Link to comment Share on other sites More sharing options...
Philip Posted July 11, 2009 Share Posted July 11, 2009 Yes, using LIKE for mysql search. What does this have to do with searching through an array? If you have another way of doing it, by all means please show me haha Quote Link to comment Share on other sites More sharing options...
Q695 Posted July 11, 2009 Share Posted July 11, 2009 I guess your method works, but I would've used the strstr (or whatever it is to find spaces) to break up the text until the whole string is used, but your method works well. Quote Link to comment Share on other sites More sharing options...
vineld Posted July 11, 2009 Author Share Posted July 11, 2009 Thanks for the replies although I have no idea what you are talking about, Q695? Are you talking about a database now? Please explain further. Quote Link to comment Share on other sites More sharing options...
haku Posted July 11, 2009 Share Posted July 11, 2009 You don't know what he's talking about because he doesn't know what he's talking about. He's talking about mysql, and you are talking about PHP. Just ignore him. Quote Link to comment Share on other sites More sharing options...
Q695 Posted July 11, 2009 Share Posted July 11, 2009 I'm talking about querying databases to search for data sets. Quote Link to comment Share on other sites More sharing options...
haku Posted July 11, 2009 Share Posted July 11, 2009 I know you are. And you are the only one in this thread who is. Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 11, 2009 Share Posted July 11, 2009 I know you are. And you are the only one in this thread who is. Yeah, for a minute there (well, at least a second or two) I thought I was missing something. Here are a couple quick functions you could use building upon what haku posted function allInArray($needleArray, $haystackArray) { //Returns true or false based on if all the values in the //needle array are found inthe haystack array return (count(array_diff($needleArray, $haystackArray)) == 0); } function someInArray($needleArray, $haystackArray) { //Returns false if no values are found in haystack array //or an array of the values found in the haystack array $found = array_intersect($needleArray, $haystackArray); return (count($found)==0) ? false : $found; } Not tested Quote Link to comment Share on other sites More sharing options...
haku Posted July 11, 2009 Share Posted July 11, 2009 Here are a couple quick functions you could use building upon what haku posted While I would love to take credit, alas, it was not I! Quote Link to comment 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.