How to create a unique string from php array

I need a unique string from an array so that I can determine when it will change without measuring the inputs of this array. I am trying to work if it is computationally efficient to calculate the value, rather than adding code to keep track of changes in the array. The array itself can have many values ​​for future verification. I don’t want to try to measure whether new values ​​have been added to the array, I would 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 used md5(http_build_query($a)), perhaps with the added one ksort, to confirm that the order of the keys has not changed, this can lead to the creation of a unique string that I can use to compare with another application run to evaluate whether the array has changed.

I am looking for alternative, possibly faster or more elegant solutions.

+5
source share
4 answers

Thanks for all the guys ideas.

I tried all of them except sha-256, which my server did not install.

Here are 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

1000 . , http_build_query . , , - - ?

:

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();
+2

Im md5(serialize($array)) . , .

+7

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

, .

, , .

+1

All Articles