Jaswinder Posted May 30, 2014 Share Posted May 30, 2014 Hi guys , Again i facing array comparison problem i got this after var_dump() $a and $b array(4) { ["q1"]=> string(3) "yes" ["q4"]=> string(2) "no" ["q3"]=> string(2) "no" ["q2"]=> string(2) "no" }array(4) { ["q1"]=> string(2) "no" ["q4"]=> string(3) "yes" ["q3"]=> string(3) "yes" ["q2"]=> string(3) "yes" } But when i use array_intersect($a,$b).. it results this array(4) { ["q1"]=> string(3) "yes" ["q4"]=> string(2) "no" ["q3"]=> string(2) "no" ["q2"]=> string(2) "no" } Don't know why, but there is no same value in both Quote Link to comment Share on other sites More sharing options...
Solution requinix Posted May 30, 2014 Solution Share Posted May 30, 2014 (edited) array_intersect() only checks values - not keys. Maybe you want array_intersect_assoc? Edited May 30, 2014 by requinix Quote Link to comment Share on other sites More sharing options...
Jaswinder Posted May 30, 2014 Author Share Posted May 30, 2014 thanks for reply, array_intersect_assoc() works But i am not clear, As you said - array_intersect() only checks values - not keys and here i am trying to compare the values only, and all values are different, so why i cant be able to use array_intersect() here ? Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted May 30, 2014 Share Posted May 30, 2014 (edited) You're confused. The values are not different: Both arrays contain the strings “yes” and “no”, and that's exactly what array_intersect() tells you. The only thing that's different are the associations between the keys and the values. If you care about the associations and not just the values, then you need the already mentioned array_intersect_assoc(). Actually, the correct solution would be to use a query and not fumble with those arrays in the first place. This “yes”/“no” stuff is also more than questionable. We have booleans for this. Edited May 30, 2014 by Jacques1 Quote Link to comment Share on other sites More sharing options...
Jaswinder Posted May 30, 2014 Author Share Posted May 30, 2014 Hey thanks for clearing me. i knew there are booleans but i need yes/no in my project Quote Link to comment Share on other sites More sharing options...
kicken Posted May 30, 2014 Share Posted May 30, 2014 (edited) so why i cant be able to use array_intersect() here ? array_intersect only checks the values, without regard to which keys they are assigned. That is why you get the result you do. Array intersect works something like this: function array_intersect(){ $allArrays = func_get_args(); $firstArray = array_shift($allArrays); $output = array(); foreach ($firstArray as $key=>$value){ //Check if the value exists in the other arrays $exists = true; foreach ($allArrays as $testArray){ if (!in_array($value, $testArray)){ $exists=false; break; } } if ($exists){ $output[$key] = $value; } } return $output; } It doesn't matter which key contains the value in the subsequent array. If it exists at all then that key/value pair is returned in the new array. Essentially this means your array consists of only two elements, 'yes' and 'no', both of which are contained in the second array so you get back a copy of the first array. array_intersect_assoc will take the keys into account and as such return the proper result of nothing for your case. For each item in the original array it will check if any of the subsequent items have that key, and if that key's value is the same as the original value. As mentioned by Barand in your previous thread however, if all you want to know is whether they are the same or not then you can simply use the == operator. The array_intersect_assoc is only necessary if you need to do something with the common elements. Edited May 30, 2014 by kicken Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted May 30, 2014 Share Posted May 30, 2014 (edited) i knew there are booleans but i need yes/no in my project Says who? The problem with abusing strings as pseudo-booleans is that it creates ambiguity and can easily end in total chaos. It's simply bad design. If this is a VARCHAR field, then your database system will accept any string. Could be “yes” or “YES!” or “true” or “iliketurtles”. Sure, “this shouldn't happen”™. But depending on who enters the data and how the rest of your application looks like, it can happen. And then good luck cleaning up the mess. This “yes”/“no” thing may be OK for an Excel spreadsheet. But when you're dealing with a real database system, you should use real data types and not rely on magic strings. It also seems you're confusing two different things: How you store the data has nothing to do with how you display it. Of course you can display the answers as “yes”/“no” on your page. But you store them as booleans. Edited May 30, 2014 by Jacques1 Quote Link to comment Share on other sites More sharing options...
Jaswinder Posted May 30, 2014 Author Share Posted May 30, 2014 Thanks kicken and Jacques1 Both of you helped me in learning new. 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.