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. Link to comment https://forums.phpfreaks.com/topic/63809-solved-how-would-i-do-this/ 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. Link to comment https://forums.phpfreaks.com/topic/63809-solved-how-would-i-do-this/#findComment-317995 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); Link to comment https://forums.phpfreaks.com/topic/63809-solved-how-would-i-do-this/#findComment-318003 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? Link to comment https://forums.phpfreaks.com/topic/63809-solved-how-would-i-do-this/#findComment-318019 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 Link to comment https://forums.phpfreaks.com/topic/63809-solved-how-would-i-do-this/#findComment-318023 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. Link to comment https://forums.phpfreaks.com/topic/63809-solved-how-would-i-do-this/#findComment-318028 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 Link to comment https://forums.phpfreaks.com/topic/63809-solved-how-would-i-do-this/#findComment-318029 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. Link to comment https://forums.phpfreaks.com/topic/63809-solved-how-would-i-do-this/#findComment-318045 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 } Link to comment https://forums.phpfreaks.com/topic/63809-solved-how-would-i-do-this/#findComment-318049 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 Link to comment https://forums.phpfreaks.com/topic/63809-solved-how-would-i-do-this/#findComment-318066 Share on other sites More sharing options...
teng84 Posted August 8, 2007 Share Posted August 8, 2007 time marking this as solved Link to comment https://forums.phpfreaks.com/topic/63809-solved-how-would-i-do-this/#findComment-318069 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.