Jump to content

Array comparison problem


Jaswinder

Recommended Posts

Hi

i am trying to compare to arrays

 

$a = Array ( [q1] => no [q3] => yes [q2] => yes );

$b = Array ( [q1] => no [q3] => yes [q2] => yes );

 

But when i intersect them it results in one element only, dont know why???

 

$c = array_intersect($a,$b);

print_r($c);

 

output -

 

Array ( [q1] => no )

Link to comment
https://forums.phpfreaks.com/topic/288770-array-comparison-problem/
Share on other sites

Allow me to start by asking if intended for the array values to be constants?

 

Please perform an experiment where the keys and values are quoted, and the elements are properly separated by commas, and that you are properly declaring $a and $b as array().

 

I gather what you have posted above is a plain-text approximation of what the arrays contain - not syntactically correct PHP statements.

If you just want to know if the keys and values are the same a simple equality check will do it

$a = array('q1' => 'no', 'q3' => 'yes', 'q2' => 'yes');
$b = array('q1' => 'no', 'q2' => 'yes', 'q3' => 'yes');

echo $a == $b ? "Arrays are the same" : "Arrays are different";

Hi thanks for reply guys, nothing helping,

I explain you what i am doing

 

I entered some questions in database with answer  yes    or   no

 

After that  i asked the same question from visitors and get their answer also .., then i compared them ,

 

Now in the same example , if i take some numeric values, instead of yes/no, the functions results correct i.e returning all the same values.

 

i think character comparison is giving problem ? isn't it ?

i tried var_dump() , and i found  some problem

 

array(3) { ["q1"]=> string(2) "no" ["q2"]=> string(10) " yes" ["q3"]=> string(10) " yes" }

array(3) { ["q1"]=> string(2) "no" ["q2"]=> string(3) "yes" ["q3"]=> string(3) "yes" }

 

After array_intersect()

array(1) { ["q1"]=> string(2) "no" }

 

Why it showing string(10), there is only 3

 

I think there is something messy in the value of input field

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.