Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.