Jump to content

Array comparison problem


Jaswinder
Go to solution Solved by 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
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.

Edited by bsmither
Link to comment
Share on other sites

It should be outputting all 3.

<pre>
<?php
$a = ['q1' => 'no', 'q3' => 'yes', 'q2' => 'yes'];
$b = ['q1' => 'no', 'q3' => 'yes', 'q2' => 'yes'];

print_r(array_intersect($a, $b));

Returns:

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

Can you post your actual code?

Link to comment
Share on other sites

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";
Link to comment
Share on other sites

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 ?

Link to comment
Share on other sites

  • Solution

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

Edited by Jaswinder
Link to comment
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.