Jaswinder Posted May 26, 2014 Share Posted May 26, 2014 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 ) Quote Link to comment Share on other sites More sharing options...
bsmither Posted May 26, 2014 Share Posted May 26, 2014 (edited) 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 May 26, 2014 by bsmither Quote Link to comment Share on other sites More sharing options...
Philip Posted May 26, 2014 Share Posted May 26, 2014 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? Quote Link to comment Share on other sites More sharing options...
bsmither Posted May 26, 2014 Share Posted May 26, 2014 My experiment confirms Philip. I am running PHP 5.3.5. Quote Link to comment Share on other sites More sharing options...
Barand Posted May 26, 2014 Share Posted May 26, 2014 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"; Quote Link to comment Share on other sites More sharing options...
Jaswinder Posted May 27, 2014 Author Share Posted May 27, 2014 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 ? Quote Link to comment Share on other sites More sharing options...
Jaswinder Posted May 27, 2014 Author Share Posted May 27, 2014 Any further help ? Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted May 27, 2014 Share Posted May 27, 2014 use var_dump() on your two arrays being compared, that would help show things like white-space in with the data. Quote Link to comment Share on other sites More sharing options...
Solution Jaswinder Posted May 28, 2014 Author Solution Share Posted May 28, 2014 (edited) 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 May 28, 2014 by Jaswinder Quote Link to comment Share on other sites More sharing options...
Jaswinder Posted May 28, 2014 Author Share Posted May 28, 2014 (edited) Hey thanks guys , i found why its coming, there were spaces in the value of my input fields and this is the most silly mistake i ever done. Edited May 28, 2014 by Jaswinder Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.