Jump to content

[SOLVED] find common in php arrays


gamesnepal

Recommended Posts

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.