Unseeeen Posted August 7, 2007 Share Posted August 7, 2007 So basically I have 2 lists of information. ex: list 1- bob, joe, dan, billy list 2- paul, joe, jim, mike After putting both of those lists into 2 text boxes, how would I make it so only the common ones show up? In this case joe would be the only name that should show up. Thanks. Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted August 7, 2007 Share Posted August 7, 2007 make arrays from both lists using explode(); array_intersect(); will then tell you which elements match up - use that result to put the matching name(s) inot the list boxes you need. Quote Link to comment Share on other sites More sharing options...
teng84 Posted August 8, 2007 Share Posted August 8, 2007 $array1= explode(', ','bob, joe, dan, billy'); $array2= explode(', ','paul, joe, jim, mike'); $result = array_intersect($array1, $array2); print_r($result); Quote Link to comment Share on other sites More sharing options...
Unseeeen Posted August 8, 2007 Author Share Posted August 8, 2007 Thanks guys, that helps a lot. Here's my code... <form action="" method="post"> <textarea name="first"></textarea><br> <textarea name="second"></textarea> <input value="Submit" type="submit"> </form> <? $first = $_POST['first']; $second = $_POST['second']; $blah = explode(" ", $first); $blahtwo = explode(" ", $second); $result = array_intersect($blah, $blahtwo); print_r($result); ?> It's simple, the way I like it. Anyways, how would I echo the info without having that "Array ( [0] => joe )" thing at the end? Quote Link to comment Share on other sites More sharing options...
teng84 Posted August 8, 2007 Share Posted August 8, 2007 first use $x=count($result); this count how many matches found then loop for($i=0; $1<$x; $i++) { echo $result[$i];// assign in the variable } but if your sure that there will only be one match echo $result[0]; this is how to replace the print_r Quote Link to comment Share on other sites More sharing options...
Unseeeen Posted August 8, 2007 Author Share Posted August 8, 2007 There can only be one match per person, but there will be multiple people. My code works exactly how I'd like it. The result just annoys me though. Quote Link to comment Share on other sites More sharing options...
teng84 Posted August 8, 2007 Share Posted August 8, 2007 use this for($i=0; $1<$x; $i++) { echo $result[$i];// assign in the variable } and what annoys you Quote Link to comment Share on other sites More sharing options...
Unseeeen Posted August 8, 2007 Author Share Posted August 8, 2007 I get an error with that... Parse error: syntax error, unexpected T_LNUMBER, expecting T_VARIABLE or '$' I don't really understand what that's trying to do anyways. But what I mean by it annoys me, the result is in an array. I don't want it in an array. Quote Link to comment Share on other sites More sharing options...
teng84 Posted August 8, 2007 Share Posted August 8, 2007 <form action="" method="post"> <textarea name="first"></textarea><br> <textarea name="second"></textarea> <input value="Submit" type="submit"> </form> <? $first = $_POST['first']; $second = $_POST['second']; $blah = explode(" ", $first); $blahtwo = explode(" ", $second); $result = array_intersect($blah, $blahtwo); for($i=0; $i<count($result); $i++) { echo $result[$i];// assign in the variable } Quote Link to comment Share on other sites More sharing options...
Unseeeen Posted August 8, 2007 Author Share Posted August 8, 2007 That's awesome. Thanks for all the help Quote Link to comment Share on other sites More sharing options...
teng84 Posted August 8, 2007 Share Posted August 8, 2007 time marking this as solved 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.