简体   繁体   English

如何从php数组生成一个唯一的字符串

[英]How to produce a unique string from a php array

I need a unique string from an array so that I can tell when it changes without measuring the inputs of that array. 我需要一个来自数组的唯一字符串,以便我可以在不测量该数组的输入的情况下判断它何时发生变化。 I'm trying to work out if it is computationally efficient to calculate a value rather than add code to look out for changes in the array. 我正在尝试计算一个计算值而不是添加代码以查找数组中的更改的计算效率。 The array itself can have a variety of values and for future proofing I don't want to try and measure whether new values have been added to the array, I'd much rather just create some string or hash that will change if the array itself changes. 数组本身可以有各种各样的值,为了将来打样,我不想尝试测量是否已将新值添加到数组中,我更愿意创建一些字符串或散列,如果数组本身会改变变化。

So for example: 例如:

$a = Array(
'var1' => 1,
'var2' => 2,
'var3' => 3,
);

If I was to use md5(http_build_query($a)) perhaps with an added ksort to confirm that the order of the keys haven't changed this might then produce a unique string that I can use to compare against another run of the application to evaluate whether the array has changed. 如果我使用md5(http_build_query($a))或许添加了ksort来确认密钥的顺序没有改变,那么可能会生成一个唯一的字符串,我可以用来与另一个应用程序的运行进行比较评估阵列是否已更改。

I'm looking for an alternate, possibly faster or more elegant solutions to this. 我正在寻找替代的,可能更快或更优雅的解决方案。

Im use md5(serialize($array)) for this. 我为此使用md5(serialize($array)) Its better, because works for multi-dimensional arrays. 它更好,因为适用于多维数组。

PHP has an array_diff() function, don't know if it's of any use for you. PHP有一个array_diff()函数,不知道它对你有用。

Otherwise, you can eventualy use the incremental hashing possibility offered by php : http://www.php.net/manual/en/function.hash-init.php by iterating over each values of the array and adding them in the incremental hash. 否则,你可以最终使用php提供的增量散列可能性: http ://www.php.net/manual/en/function.hash-init.php迭代遍历数组的每个值并将它们添加到增量散列中。

Thanks for all the ideas guys. 感谢所有的想法。

I've tried all of them except a sha-256 which my server doesn't have installed. 我已经尝试了所有这些,除了我的服务器没有安装的sha-256。

Here's the results: 结果如下:

Average (http_build_query): 1.3954045954045E-5
Average (diff): 0.00011533766233766
Average (serialize): 1.7588411588412E-5
Average (md5): 1.6036963036966E-5
Average (implode-haval160,4): 1.5349650349649E-5

That's running the operation 1000 times and averaging the result. 这是运行1000次并平均结果的操作。 After refreshing a couple times I could tell that the http_build_query was the quickest. 刷新几次后,我可以看出http_build_query是最快的。 I guess my next question would be if anyone can think of any pitfalls of using this method? 我想我的下一个问题是,如果有人能想到使用这种方法的任何陷阱吗?

Thanks 谢谢

Here's my code: 这是我的代码:

class a {

    static $input;

    function test() {
        $start = null;
        $s = $e = $d = $g = $h = $i = $k = array();
        self::$input = array();

        for ($x = 0; $x <= 30; $x++) {
            self::$input['variable_' . $x] = rand();
        }

        for ($x = 0; $x <= 1000; $x++) {
            $start = microtime();

            $c = http_build_query(self::$input);
            ($c == $c);

            $s[] = microtime() - $start;
        }

        for ($x = 0; $x <= 1000; $x++) {
            $start = microtime();

            $c = md5(http_build_query(self::$input));
            ($c == $c);

            $e[] = microtime() - $start;
        }

        for ($x = 0; $x <= 1000; $x++) {
            $start = microtime();

            $c = array_diff(self::$input, self::$input);

            $d[] = microtime() - $start;
        }
        for ($x = 0; $x <= 1000; $x++) {
            $start = microtime();

            $c = serialize(self::$input);
            ($c == $c);

            $g[] = microtime() - $start;
        }

        for ($x = 0; $x <= 1000; $x++) {
            $start = microtime();

            $c =  hash("haval160,4", implode(',',self::$input));
            ($c == $c);

            $h[] = microtime() - $start;
        }
        echo "<pre>";

//print_r($s);
        echo "Average (http_build_query): " . array_sum($s) / count($s) . "<br>";
        echo "Average (diff): " . array_sum($d) / count($d) . "<br>";
        echo "Average (serialize): " . array_sum($g) / count($g) . "<br>";
        echo "Average (md5): " . array_sum($e) / count($e). "<br>";
        echo "Average (implode-haval160,4): " . array_sum($h) / count($h);
    }

}

a::test();

You could always just do 你可以随时做

$str = implode(",", $a);
$check = hash("sha-256", $str);

Theoretically, that should detect changes in array size, data, or ordering. 从理论上讲,它应该检测数组大小,数据或排序的变化。

Of course, you can use whatever hash you wish. 当然,你可以使用你想要的任何哈希。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM