dream04110 2015-12-19 20:22
浏览 111
已采纳

php array_search返回非匹配元素

When I try to find collision in a hash value using php, there is a problem. I can find the first 3 byte collision successfully. However when I try to find the 10 byte collision the array_search does not work. It return some non match value to me. Can anyone help me? Here is my code.

<?php 
$array = array();
$do = true;
do {
  $num1= rand(1,90000);
  $num2= rand(1,90000);

  $testcase2 = $num1."14040371D".$num2;
  $testcase1 = (string)$testcase2;
  $hashed = (string)substr(sha1($testcase1),0,10);
  //echo($testcase1."
");
  $findornot = array_search($hashed,$array); 

  if($findornot !== false) {
    $array[$testcase1] = $hashed; 
    echo ($findornot."
"); 
    echo ($testcase1."
"); 
    echo ($hashed);

    $do=false;
  } else {
    $array[$testcase1] = $hashed; 
  }
  //print_r(array_values($array));
}while($do)
?>
  • 写回答

1条回答 默认 最新

  • duanhao5038 2015-12-19 23:31
    关注

    TL;DR

    $findornot = array_search($hashed, $array, true);
    

    Fixed.


    Explanation:

    You're not going crazy.

    The key is "strict comparison" - you've correctly type casted in a few places, and used === for your own comparisons.

    You need array_search to do the same thing. Otherwise a string like 0e1 will match 0e2 according to array_search.

    The third parameter to array_search is an optional boolean to force strict comparison. Turn that on, and your problem will go away.


    Update (to prove)

    Proof code (input):

    <?php
    $a = "2e1";
    $b = "2e2";
    
    var_dump($a == $b);
    var_dump($a === $b);
    var_dump($a == "20");
    var_dump($a === "20");
    

    Proof output:

    bool(false)
    bool(false)
    bool(true)
    bool(false)
    

    PHP is seeing the 'e' character surrounded by numeric digits and is treating it as a exponential when type-juggling to an integer.

    It's one of PHP's best features, and one of its greatest failings IMHO. Ah well! Anyone up for coding in C?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型