PHP MD5 多维数组的最佳方式? [英] PHP best way to MD5 multi-dimensional array?

查看:31
本文介绍了PHP MD5 多维数组的最佳方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

生成多维数组的 MD5(或任何其他散列)的最佳方法是什么?

What is the best way to generate an MD5 (or any other hash) of a multi-dimensional array?

我可以轻松编写一个循环,遍历数组的每一层,将每个值连接成一个字符串,然后简单地对字符串执行 MD5.

I could easily write a loop which would traverse through each level of the array, concatenating each value into a string, and simply performing the MD5 on the string.

然而,这充其量看起来很麻烦,我想知道是否有一个时髦的函数可以接受多维数组并对其进行哈希处理.

However, this seems cumbersome at best and I wondered if there was a funky function which would take a multi-dimensional array, and hash it.

推荐答案

(底部可复制粘贴功能)

如前所述,以下内容将起作用.

As mentioned prior, the following will work.

md5(serialize($array));

然而,值得注意的是(具有讽刺意味的是)json_encode 的执行速度明显:

However, it's worth noting that (ironically) json_encode performs noticeably faster:

md5(json_encode($array));

实际上,这里的速度提高了两倍,因为 (1) json_encode 单独执行比序列化快,并且 (2) json_encode 生成更小的字符串,因此 md5 处理的字符串更少.

In fact, the speed increase is two-fold here as (1) json_encode alone performs faster than serialize, and (2) json_encode produces a smaller string and therefore less for md5 to handle.

以下是支持此声明的证据:

Here is evidence to support this claim:

<?php //this is the array I'm using -- it's multidimensional.
$array = unserialize('a:6:{i:0;a:0:{}i:1;a:3:{i:0;a:0:{}i:1;a:0:{}i:2;a:3:{i:0;a:0:{}i:1;a:0:{}i:2;a:0:{}}}i:2;s:5:"hello";i:3;a:2:{i:0;a:0:{}i:1;a:0:{}}i:4;a:1:{i:0;a:1:{i:0;a:1:{i:0;a:1:{i:0;a:1:{i:0;a:1:{i:0;a:0:{}}}}}}}i:5;a:5:{i:0;a:0:{}i:1;a:4:{i:0;a:0:{}i:1;a:0:{}i:2;a:3:{i:0;a:0:{}i:1;a:0:{}i:2;a:0:{}}i:3;a:6:{i:0;a:0:{}i:1;a:3:{i:0;a:0:{}i:1;a:0:{}i:2;a:3:{i:0;a:0:{}i:1;a:0:{}i:2;a:0:{}}}i:2;s:5:"hello";i:3;a:2:{i:0;a:0:{}i:1;a:0:{}}i:4;a:1:{i:0;a:1:{i:0;a:1:{i:0;a:1:{i:0;a:1:{i:0;a:1:{i:0;a:0:{}}}}}}}i:5;a:5:{i:0;a:0:{}i:1;a:3:{i:0;a:0:{}i:1;a:0:{}i:2;a:3:{i:0;a:0:{}i:1;a:0:{}i:2;a:0:{}}}i:2;s:5:"hello";i:3;a:2:{i:0;a:0:{}i:1;a:0:{}}i:4;a:1:{i:0;a:1:{i:0;a:1:{i:0;a:1:{i:0;a:1:{i:0;a:1:{i:0;a:0:{}}}}}}}}}}i:2;s:5:"hello";i:3;a:2:{i:0;a:0:{}i:1;a:0:{}}i:4;a:1:{i:0;a:1:{i:0;a:1:{i:0;a:1:{i:0;a:1:{i:0;a:1:{i:0;a:0:{}}}}}}}}}');

//The serialize test
$b4_s = microtime(1);
for ($i=0;$i<10000;$i++) {
    $serial = md5(serialize($array));
}
echo 'serialize() w/ md5() took: '.($sTime = microtime(1)-$b4_s).' sec<br/>';

//The json test
$b4_j = microtime(1);
for ($i=0;$i<10000;$i++) {
    $serial = md5(json_encode($array));
}
echo 'json_encode() w/ md5() took: '.($jTime = microtime(1)-$b4_j).' sec<br/><br/>';
echo 'json_encode is <strong>'.( round(($sTime/$jTime)*100,1) ).'%</strong> faster with a difference of <strong>'.($sTime-$jTime).' seconds</strong>';

JSON_ENCODE 始终快 250% (2.5x) 以上(通常超过 300%)——这不是一个微不足道的区别.您可以在此处查看此实时脚本的测试结果:

JSON_ENCODE is consistently over 250% (2.5x) faster (often over 300%) -- this is not a trivial difference. You may see the results of the test with this live script here:

现在,需要注意的一件事是 array(1,2,3) 将产生与 array(3,2,1) 不同的 MD5.如果这不是您想要的.试试下面的代码:

Now, one thing to note is array(1,2,3) will produce a different MD5 as array(3,2,1). If this is NOT what you want. Try the following code:

//Optionally make a copy of the array (if you want to preserve the original order)
$original = $array;

array_multisort($array);
$hash = md5(json_encode($array));

存在一些问题,即颠倒顺序是否会产生相同的结果.所以,我在这里做了(正确):

There's been some question as to whether reversing the order would produce the same results. So, I've done that (correctly) here:

如您所见,结果完全相同.这是(更正)测试最初由与 Drupal 相关的人创建:

As you can see, the results are exactly the same. Here's the (corrected) test originally created by someone related to Drupal:

为了更好的衡量,这里有一个您可以复制和粘贴的函数/方法(在 5.3.3-1ubuntu9.5 中测试):

function array_md5(Array $array) {
    //since we're inside a function (which uses a copied array, not 
    //a referenced array), you shouldn't need to copy the array
    array_multisort($array);
    return md5(json_encode($array));
}

这篇关于PHP MD5 多维数组的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆