mac_gabe Posted July 7, 2011 Share Posted July 7, 2011 Hi, I'm trying to use in_array to match elements in 2 arrays, but I can't get it to work. To simplify, I have 2 arrays, each contain a list (in fact the lists have about 250 items) $complete - good order $partial - bad order Horse Cat Dog Elephant Cat Dog Elephant I want to print the complete list, in the good order, but if the item is also in the partial list, to append a x (in the real example I'll actually be making html links with preg_replace instead of appending x) So, final product: Horse Dog x Cat x Elephant x I have this: $y= count($complete) -1; for ($u=1; $u < $y; $u++) { if (in_array ($complete[$u], $partial)) { print "<li>".$complete[$u].' x</li>'; } else { print "<li>".$complete[$u]."</li>"; } But it seems to print the complete list with no changes - i.e. it's finding no matches. Thanks for any help - if there's a better method than in_array(), I'd be happy to use it. Quote Link to comment https://forums.phpfreaks.com/topic/241314-help-with-in_array-beginner-level/ Share on other sites More sharing options...
monkeytooth Posted July 7, 2011 Share Posted July 7, 2011 If your just trying to find matches. You can use array_diff() http://php.net/manual/en/function.array-diff.php You take 2 arrays (or more I think if you want) $array1 = array(1,2,3,4,5,6,7,8,9,a,b,c); $array2 = array(z,y,x,9,8,7,6,5,4,3,2,1); $difference = array_diff($array1, $array2); sort($difference); echo "<pre>"; print_r($difference); echo "</pre>"; and what $difference should output is.. Array( [] = a, [] = b, [] = c, [] = x, [] = y, [] = z, ) which you could then know by this array that those aren't matches between the 2. You can definitively expand on this a lot more to then turn around and rebuild a final array where its either just the matches, or the difference + the matches but removing the duplicates.. but this here is like the stepping stone Idea from what I am understanding your trying to do. Quote Link to comment https://forums.phpfreaks.com/topic/241314-help-with-in_array-beginner-level/#findComment-1239559 Share on other sites More sharing options...
Sarah07 Posted July 7, 2011 Share Posted July 7, 2011 monkeytooth beat me to it but still just though I would say the little that I knew. <?php $complete = array('Horse','Snake','Cat','Dog','Elephant'); $partial = array('Cat','Elephant','Dog'); $matched_items = array_intersect($complete, $partial); $unmatched_items = array_diff($complete, $partial); print_r ($unmatched_items); //prints unmatched items echo "<br/>"; print_r($matched_items); ?> Quote Link to comment https://forums.phpfreaks.com/topic/241314-help-with-in_array-beginner-level/#findComment-1239563 Share on other sites More sharing options...
mac_gabe Posted July 7, 2011 Author Share Posted July 7, 2011 Thank you both - I'm going to use your methods to check to see what's matching, which may help me to see what's going wrong. BRB. Quote Link to comment https://forums.phpfreaks.com/topic/241314-help-with-in_array-beginner-level/#findComment-1239570 Share on other sites More sharing options...
mac_gabe Posted July 7, 2011 Author Share Posted July 7, 2011 Well that's weird, using $difference = array_diff($complete, $partial); sort($difference); echo "<pre>";print_r($difference);echo "</pre>"; I get the full complete list, double spaced and if I switch it round to $difference = array_diff($partial, $complete); I get the partial list, single spaced Either way, no matches, and yet the two lists look like they have the same items. Must be formatted different somehow. Will delve deeper… Quote Link to comment https://forums.phpfreaks.com/topic/241314-help-with-in_array-beginner-level/#findComment-1239573 Share on other sites More sharing options...
mac_gabe Posted July 7, 2011 Author Share Posted July 7, 2011 It's really hard to see where those extra blank lines are coming from! This is a sample of the first few lines from my complete list: ( [0] => Acanthisittidae [1] => Acanthizidae [2] => Accipitridae [3] => Acrocephalidae [4] => Aegithalidae and here is a sample from the partial list ( [0] => Accipitridae [1] => Acrocephalidae [2] => Aegithalidae [3] => Alaudidae [4] => Alcedinidae Both as viewed in Firefox 5 -> view source Any ideas what could be creating that extra line, or how I can see what's really going on? Quote Link to comment https://forums.phpfreaks.com/topic/241314-help-with-in_array-beginner-level/#findComment-1239578 Share on other sites More sharing options...
craygo Posted July 7, 2011 Share Posted July 7, 2011 can use this <?php $complete = array("Horse", "Dog", "Cat", "Elephant"); $partial = array("Cat", "Elephant", "Dog"); foreach ($complete as $item){ if(in_array($item, $partial)){ echo $item." x<br>"; } else { echo $item."<br>"; } } ?> Ray Quote Link to comment https://forums.phpfreaks.com/topic/241314-help-with-in_array-beginner-level/#findComment-1239584 Share on other sites More sharing options...
mac_gabe Posted July 7, 2011 Author Share Posted July 7, 2011 thanks craygo, I'll bear that in mind later, but it seems now that the problem is that there are in fact no matches, because an extra white line has crept in somehow. I'm going to try and strip the whitespace/returns from the $complete first then give your code a go. Quote Link to comment https://forums.phpfreaks.com/topic/241314-help-with-in_array-beginner-level/#findComment-1239588 Share on other sites More sharing options...
TeNDoLLA Posted July 7, 2011 Share Posted July 7, 2011 You can use trim() on the values before comparing to get rid of extra spaces. Also can save the trimmed values to prevent that happening again. Quote Link to comment https://forums.phpfreaks.com/topic/241314-help-with-in_array-beginner-level/#findComment-1239589 Share on other sites More sharing options...
AyKay47 Posted July 7, 2011 Share Posted July 7, 2011 if you are looking to find matches in the two arrays, array_diff is the opposite of what you want, as it displays the differences in the two comparing arrays... You will want to use array_intersect Quote Link to comment https://forums.phpfreaks.com/topic/241314-help-with-in_array-beginner-level/#findComment-1239593 Share on other sites More sharing options...
mac_gabe Posted July 7, 2011 Author Share Posted July 7, 2011 Tendolla, thanks I'll try you method shortly. Though I've just tried this: foreach ($complete as $item){$item= preg_replace('@([\s]*)([\w]+)([\s]*)@s', "$2", $item); } and many variations on the theme … and I still seem to get that weird double spacing. It's going to be complicated to know how many letters to trim, and where, because I can't actually see where the line (if there is one) is coming from. I think I'll try exporting $complete to a new file to se what it looks like. AyKay47, thanks, in fact either of those would be OK just to get a result of some sort (currently intersect gives none, and diff gives all). Quote Link to comment https://forums.phpfreaks.com/topic/241314-help-with-in_array-beginner-level/#findComment-1239597 Share on other sites More sharing options...
mac_gabe Posted July 7, 2011 Author Share Posted July 7, 2011 yup there was a carriage return at the end of each item in $complete. trim() worked nicely, now trying the match again… Quote Link to comment https://forums.phpfreaks.com/topic/241314-help-with-in_array-beginner-level/#findComment-1239611 Share on other sites More sharing options...
mac_gabe Posted July 7, 2011 Author Share Posted July 7, 2011 yesssss! it works :-) thanks for all your help guys . In fact, turns out my original code worked fine, it was just that the carriage return was blocking the match. Anyway I learned about diff and intersect and most importantly trim! Quote Link to comment https://forums.phpfreaks.com/topic/241314-help-with-in_array-beginner-level/#findComment-1239613 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.