如何获得JSON字符串差异百分比

时间:2015-02-26 22:06:43

标签: php json compare

是否有任何逻辑/算法可以将两个JSON字符串与差异百分比进行比较。我试图用它来验证我作为JSON字符串存储的签名。

字符串1:

[
  {"lx":5,"ly":34,"mx":5,"my":33},
  {"lx":5,"ly":34,"mx":5,"my":34},
  {"lx":6,"ly":32,"mx":5,"my":34},
  {"lx":7,"ly":31,"mx":6,"my":32},
  {"lx":8,"ly":31,"mx":7,"my":31},
  {"lx":9,"ly":30,"mx":8,"my":31},
  {"lx":10,"ly":29,"mx":9,"my":30},
  {"lx":11,"ly":29,"mx":10,"my":29},
  {"lx":11,"ly":28,"mx":11,"my":29}
]

字符串2:

[
  {"lx":5,"ly":33,"mx":5,"my":32},
  {"lx":5,"ly":33,"mx":5,"my":33},
  {"lx":6,"ly":32,"mx":5,"my":33},
  {"lx":7,"ly":32,"mx":6,"my":32},
  {"lx":8,"ly":32,"mx":7,"my":32},
  {"lx":9,"ly":32,"mx":8,"my":32},
  {"lx":10,"ly":31,"mx":9,"my":32},
  {"lx":11,"ly":30,"mx":10,"my":31},
  {"lx":12,"ly":30,"mx":11,"my":30},
  {"lx":13,"ly":29,"mx":12,"my":30}
]

任何建议都将受到高度赞赏。

2 个答案:

答案 0 :(得分:0)

我没有得到您想要做的事情,但如果您只是想检查字符串相似性,可以使用similar_text php命令

示例:

$str1 = '{"lx":5,"ly":34,"mx":5,"my":33},
  {"lx":5,"ly":34,"mx":5,"my":34},
  {"lx":6,"ly":32,"mx":5,"my":34},
  {"lx":7,"ly":31,"mx":6,"my":32},
  {"lx":8,"ly":31,"mx":7,"my":31},
  {"lx":9,"ly":30,"mx":8,"my":31},
  {"lx":10,"ly":29,"mx":9,"my":30},
  {"lx":11,"ly":29,"mx":10,"my":29},
  {"lx":11,"ly":28,"mx":11,"my":29}';

  $str2 = '{"lx":5,"ly":33,"mx":5,"my":32},
  {"lx":5,"ly":33,"mx":5,"my":33},
  {"lx":6,"ly":32,"mx":5,"my":33},
  {"lx":7,"ly":32,"mx":6,"my":32},
  {"lx":8,"ly":32,"mx":7,"my":32},
  {"lx":9,"ly":32,"mx":8,"my":32},
  {"lx":10,"ly":31,"mx":9,"my":32},
  {"lx":11,"ly":30,"mx":10,"my":31},
  {"lx":12,"ly":30,"mx":11,"my":30},
  {"lx":13,"ly":29,"mx":12,"my":30}';

  $percent = 0;

  echo similar_text($str1,$str2,$percent).' equal chars'.PHP_EOL;
  echo $percent.'% similarity';

给你:

269 equal chars
78.425655976676% similarity

答案 1 :(得分:0)

可以使用levenshtein函数

计算字符串距离

太糟糕了,它仅限于不超过255个字符的字符串。