gamesnepal Posted November 14, 2009 Share Posted November 14, 2009 I am trying to find the common values between two php arrays using the function array_intersect array one is generated from a form. The user enters values to a text box. Then i do something like $gam = $_POST['game']; $game = explode("\n",$gam); and I explode the values of the text box in arrays separated by "enter" then I have another array which is predefined. Then when I compare using array_intersect it does not show all the common values this is my entire code. I enter in the text box value1 value2 value3 <?php $gam = $_POST['game']; $game = explode("\n",$gam); $singh = array("value1","value2","value3"); print_r($game); echo "<br>"; print_r($singh); echo "<br>"; $result = array_intersect($game, $singh); print_r($result); echo "<br>"; ?> then the output is like Array ( [0] => value1 [1] => value2 [2] => value3 ) Array ( [0] => value1 [1] => value2 [2] => value3 ) Array ( [2] => value3 ) It only says that the last value is the same. How do I fix this? Link to comment https://forums.phpfreaks.com/topic/181486-solved-find-common-in-php-arrays/ Share on other sites More sharing options...
sasa Posted November 14, 2009 Share Posted November 14, 2009 trim elements of $game array before use array_intersedt foreach($game as $key => $value) $game[$key] = trim($value); Link to comment https://forums.phpfreaks.com/topic/181486-solved-find-common-in-php-arrays/#findComment-957398 Share on other sites More sharing options...
gamesnepal Posted November 14, 2009 Author Share Posted November 14, 2009 Thank you very much गुरु Sasa Link to comment https://forums.phpfreaks.com/topic/181486-solved-find-common-in-php-arrays/#findComment-957480 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.