Jump to content

Array comparison problem


Jaswinder
Go to solution Solved by requinix,

Recommended Posts

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

Link to comment
Share on other sites

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 ?

Link to comment
Share on other sites

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 by Jacques1
Link to comment
Share on other sites

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 by kicken
Link to comment
Share on other sites

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 by Jacques1
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.