Arrays values not identical (but they are?)

前端 未结 3 2740
鱼传尺愫
鱼传尺愫 2021-02-20 17:47

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!

相关标签:
3条回答
  • 2021-02-20 17:56

    Check if arrays passed to array_diff() are in right order. Caught myself few times on this.

    0 讨论(0)
  • 2021-02-20 18:07

    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.

    0 讨论(0)
  • 2021-02-20 18:10

    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/

    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.

    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.

    0 讨论(0)
提交回复
热议问题