views:

182

answers:

4

When I compare two array values I see two strings that look the same. php doesn't agree.

$array1 = array('address'=>'32 Winthrop Street','state'=>'NY');
$array2 = array('address'=>'32 Winthrop Street');

$results = array_diff_assoc($array1, $array2);

var_dump($results) 
//echos ['address'] => string(18) "32 Winthrop Street" ['state']=>'NY'

Why is this?

EDIT Be advised that this isn't the actual code I'm testing, I've simplified code to illustrate my question, which is about strings being equal, not whether or not this code will run.

A: 
$array1 = ('address'=>'32 Winthrop Street','state'=>'NY');
$array2 = ('address'=>'32 Winthrop Street');

$results = array_diff_assoc($array1, $array2); // remove the ' on this line 

The ' is probably confusing the PHP engine into thinking it's a string... then add insult to injury you don't close the "string".

Looks like a typo to me.

ILMV
that was a typo, not part of actual code, but thanks
kevtrout
+8  A: 

First: your code will not even run but die in a syntax error at line 2. Here's the corrected version:

<?php
$array1 = array ('address'=>'32 Winthrop Street','state'=>'NY');
$array2 = array ('address'=>'32 Winthrop Street');

$results = array_diff_assoc($array1, $array2);

var_dump($results); 

Second: I tested this with PHP 5.2.12 and 5.3.1 and it works. It echoes:

array(1) {
  ["state"]=>
  string(2) "NY"
}

It's really just full of incorrect syntax. That's all.

Techpriester
I didn't copy code that I actually tested, I typed new code to describe my question. My question isn't that the code won't run, but why does a string that looks equal, not evaluate as equal.
kevtrout
If you don't post your actual code, we won't be able to solve your problem.
Techpriester
+3  A: 

Are the values hard-coded or from some other source? What does var_dump($array1['address'], $array2['address']) give? Maybe there are some "invisible" spaces somewhere?

binaryLV
I'd like to know what exactly you mean by "invisible spaces" ...
Techpriester
For example, two spaces (instead one space) between "32" and "Winthrop" - in HTML they are shown as a single space. Sometimes it may be hard to notice.
binaryLV
+5  A: 

Make sure your input arrays really look like that. If you're echoing data in your browser, you might miss whitespace. '32 Winthrop Street' is not the same as ' 32 Winthrop Street', for example. The same is true for your array keys.

You could $array1 = array_map('trim', $array1) and $array2 = array_map('trim', $array2) to remove leading and trailing whitespace from the values. See if that makes any difference?

You can check if they're really the same by checking if ($array1['address'] === $array2['address']). If that evaluates to false, there's a difference, you're just not seeing it (see binaryLV's answer for elaboration on a possible cause). If it evaluates to true, you might want to take a closer look at the array keys.

pinkgothic
Comparing the md5 hashes of the two strings won't do any good. Just comparing the values directly with "===" will do the job.
Techpriester
D'oh, this is true. I was, for some reason, stuck on more complex objects (probably due to dealing with DOM trees all day; not that that would work much easier, either). Fix'd!
pinkgothic
Finally decided to add this comment anyway, since I figure it might actually be useful to highlight the *actual* source of my wires crossing originally: `md5()` or other hash functions are of course a great way to bypass 'my output medium does not display this properly'. And remember, kids, don't think one thing (output) and type the other (comparison)!
pinkgothic