drnycqxwz63508434 2013-03-04 19:42
浏览 46
已采纳

缺少array_hash()函数。 无法创建数组的哈希值

Intro:

I need a function that can take an array and return a hash of it.

This should similar to spl_object_hash(), except that it returns a hash for given array.

So, far I've tried

function array_hash(array $array) {

   return spl_object_hash((object) $array); 
}

The problems

1) This algorithm isn't efficient by itself. For example, what if I pass something like this:

  $array = array(
     'foo' => 'bar',
     'bool' => false,
     'junk' => array(
         'junk1' => array('foo' => array('__test__'))
      )
  )

It won't cast nested arrays to objects.

2) Another major problem is that, spl_object_hash() returns a different hash for the same object on each new HTTP request.

The question

Again: I need a persistent hash for an array. Unlike spl_object_hash(), the will be persistent on each HTTP request. How can I do this correctly?

  • 写回答

2条回答 默认 最新

  • doufen2769 2013-03-04 19:47
    关注

    How about serializing the array first?

    md5(serialize($array));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

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

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

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

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

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

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

客服 返回
顶部