我可以让这种内爆效率更高吗?

I need a hash to compare new and old entity, i have this function but can i make this more efficient?

    /**
     * @param $entity multilevel
     * @return string
     */
    public function implodeArray($entity)
    {
        if(is_array($entity)){
            foreach($entity as $key => $value)
            {
                if(is_array($value)){
                    $entity[$key] = $this->implodeArray($value);
                }
            }
            return str_replace(' ','',implode('',$entity));
        }elseif(is_string($entity)){
            return $entity;
        }
    }

I expect reduce the time to execute this.


UPDATE: I try this and get 1200% more efficient \o/

$md5compare = md5(json_encode(array_multisort($entity)));