PHP best way to MD5 multi-dimensional array?

后端 未结 13 2012
别那么骄傲
别那么骄傲 2020-12-07 11:48

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

I could easily write a loop which would traverse through each level of the

13条回答
  •  执念已碎
    2020-12-07 12:37

    // Convert nested arrays to a simple array
    $array = array();
    array_walk_recursive($input, function ($a) use (&$array) {
        $array[] = $a;
    });
    
    sort($array);
    
    $hash = md5(json_encode($array));
    
    ----
    
    These arrays have the same hash:
    $arr1 = array(0 => array(1, 2, 3), 1, 2);
    $arr2 = array(0 => array(1, 3, 2), 1, 2);
    

提交回复
热议问题