I have two arrays. They seem to contain at least one identical set of values, but performing array_diff()
does not return anything even though I think it should! This should have been just routine code but for some reason it's not liking what I've done.
我有两个数组。它们似乎至少包含一组相同的值,但是执行array_diff()不会返回任何东西,尽管我认为应该返回!这应该只是例行代码,但出于某种原因,它不喜欢我所做的。
The weird thing is that var_dump($queue[0]);
returns String(167);
and var_dump($videos[0])
returns String(168)
.
奇怪的是var_dump($queue[0]);返回字符串(167);美元和var_dump(视频[0])返回字符串(168)。
So clearly, they must be different right?
显然,它们是不同的,对吧?
echo similar_text($queue[0]), $videos[0]);
returns 167
. What!?
回声similar_text(队列[0])美元,美元视频[0]);返回167。什么! ?
Note: These are just file names and do not represent the contents of the file.
注意:这些只是文件名,并不表示文件的内容。
Videos Array
视频数组
Array ( [0] => /var/www/downloads/j2/Dexter Season 1, 2, 3, 4, 5 & 6 + Extras (Early Cuts, Audiobooks etc) DVDRip HDTV TSV/Season 3/Dexter Season 3 Episode 04 - All in the Family.avi )
数组([0]=> /var/www/downloads/ j2s/dexter第一季、第二季、第三季、第四季、第五季、第六季+临时演员(早期剪辑、有声读物等)avi)
Queue Array
队列的数组
Array ( [0] => /var/www/downloads/j2/Dexter Season 1, 2, 3, 4, 5 & 6 + Extras (Early Cuts, Audiobooks etc) DVDRip HDTV TSV/Season 3/Dexter Season 3 Episode 04 - All in the Family.avi [1] => j2 )
数组([0]=> /var/www/downloads/ j2s/dexter第一季、第二季、第三季、第四季、第五季、第六季+临时演员(早期剪辑、有声读物等)avi [1] => j2)
Outputs
输出
$diff = array_intersect($queue,$videos); print_r($diff);
returns Array ( )
$ diff = array_intersect(队列,视频美元);print_r(diff);返回数组()
var_dump($queue[0]);
returns string(167) "/var/www/downloads/j2/Dexter Season 1, 2, 3, 4, 5 & 6 + Extras (Early Cuts, Audiobooks etc) DVDRip HDTV TSV/Season 3/Dexter Season 3 Episode 04 - All in the Family.avi"
var_dump()美元队列[0]);返回字符串(167)“/var/www/下载/j2/Dexter第一季、第二季、第三季、第四季、第五季和第六季的额外内容(早期剪辑、有声读物等)DVDRip HDTV TSV/第三季/Dexter第三季第四季-全在我们家里。avi”
var_dump($videos[0]);
returns string(168) "/var/www/downloads/j2/Dexter Season 1, 2, 3, 4, 5 & 6 + Extras (Early Cuts, Audiobooks etc) DVDRip HDTV TSV/Season 3/Dexter Season 3 Episode 04 - All in the Family.avi"
var_dump()美元的视频[0]);返回字符串(168)“/var/www/下载/j2/Dexter第一季、第二季、第三季、第四季、第五季和第六季的额外内容(早期剪辑、有声读物等)DVDRip HDTV TSV/第三季/Dexter第三季第四季-全在我们家里。avi”
echo similar_text($queue[0], $videos[0]);
returns 167
.
回声similar_text(队列[0],美元视频[0]);返回167。
I've put the strings into JavaScript character counts, I've used strlen(), trim() to trim whitespace, I've even manually counted each character individually. What's going on?
我已经将字符串放入JavaScript字符计数中,我使用了strlen()、trim()来修饰空格,我甚至还单独手工计算了每个字符。这是怎么呢
3 个解决方案
#1
5
After converting both strings to hex-escaped form using
将两个字符串转换为十六进制转义形式之后
var_dump(preg_replace_callback('#.#', function($m) {
return '\\x' . dechex(ord($m[0]));
}, $input))
, the result strings appear like this: http://jsfiddle.net/mgaWn/
,结果字符串如下:http://jsfiddle.net/mgaWn/。
Looking at them in that form shows that the first string contains 5,·6·+·Extras
, the second one contains 5,·6··+·Extras
- there's a double space before the +
sign.
从表中可以看出,第一个字符串包含5,·6·+·附加,第二个字符串包含5,·6···+·附加——在+符号之前有一个双空格。
HTML collapses whitespace and this difference becomes completely invisible. It is generally a good idea to compare the data as close to its original format as possible, before any output format specifics (such as character encodings or this HTML whitespace minimization) get in your way.
HTML压缩空白,这种差异变得完全不可见。通常,在任何输出格式细节(如字符编码或HTML空格最小化)妨碍您之前,最好尽可能将数据与原始格式进行比较。
#2
3
There is probably a character that is non-printable.
可能有一个字符是不可打印的。
Write out both strings into a file, from PHP, in binary format, and compare the results with a hex editor or similar. Just copying the strings and then comparing will not do for some cases as it might lose characters.
用二进制格式将这两个字符串写进一个文件,并与十六进制编辑器或类似的编辑器比较结果。仅仅复制字符串然后进行比较在某些情况下是不行的,因为它可能会丢失字符。
#3
1
Check if arrays passed to array_diff() are in right order. Caught myself few times on this.
检查传递给array_diff()的数组是否正确。在这件事上我被逮了几次。
#1
5
After converting both strings to hex-escaped form using
将两个字符串转换为十六进制转义形式之后
var_dump(preg_replace_callback('#.#', function($m) {
return '\\x' . dechex(ord($m[0]));
}, $input))
, the result strings appear like this: http://jsfiddle.net/mgaWn/
,结果字符串如下:http://jsfiddle.net/mgaWn/。
Looking at them in that form shows that the first string contains 5,·6·+·Extras
, the second one contains 5,·6··+·Extras
- there's a double space before the +
sign.
从表中可以看出,第一个字符串包含5,·6·+·附加,第二个字符串包含5,·6···+·附加——在+符号之前有一个双空格。
HTML collapses whitespace and this difference becomes completely invisible. It is generally a good idea to compare the data as close to its original format as possible, before any output format specifics (such as character encodings or this HTML whitespace minimization) get in your way.
HTML压缩空白,这种差异变得完全不可见。通常,在任何输出格式细节(如字符编码或HTML空格最小化)妨碍您之前,最好尽可能将数据与原始格式进行比较。
#2
3
There is probably a character that is non-printable.
可能有一个字符是不可打印的。
Write out both strings into a file, from PHP, in binary format, and compare the results with a hex editor or similar. Just copying the strings and then comparing will not do for some cases as it might lose characters.
用二进制格式将这两个字符串写进一个文件,并与十六进制编辑器或类似的编辑器比较结果。仅仅复制字符串然后进行比较在某些情况下是不行的,因为它可能会丢失字符。
#3
1
Check if arrays passed to array_diff() are in right order. Caught myself few times on this.
检查传递给array_diff()的数组是否正确。在这件事上我被逮了几次。