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

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 15: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?

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部