PHP best way to MD5 multi-dimensional array?

后端 未结 13 2061
别那么骄傲
别那么骄傲 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:30

    I think that this could be a good tip:

    Class hasharray {
    
        public function array_flat($in,$keys=array(),$out=array()){
            foreach($in as $k => $v){
                $keys[] = $k; 
                if(is_array($v)){
                    $out = $this->array_flat($v,$keys,$out);
                }else{
                    $out[implode("/",$keys)] = $v;
                }
                array_pop($keys);
            }
            return $out;  
        }
    
        public function array_hash($in){
            $a = $this->array_flat($in);
            ksort($a);
            return md5(json_encode($a));
        }
    
    }
    
    $h = new hasharray;
    echo $h->array_hash($multi_dimensional_array);
    

提交回复
热议问题