php 下划线和驼峰相互转换

/*
 * 下划线转驼峰
 */
private function convertUnderline($str)
{
    $str = preg_replace_callback('/([-_]+([a-z]{1}))/i',function($matches){
        return strtoupper($matches[2]);
    },$str);
    return $str;
}

/*
 * 驼峰转下划线
 */
private function humpToLine($str){
    $str = preg_replace_callback('/([A-Z]{1})/',function($matches){
        return '_'.strtolower($matches[0]);
    },$str);
    return $str;
}

private function convertHump(array $data){
    $result = [];
    foreach ($data as $key => $item) {
        if (is_array($item) || is_object($item)) {
            $result[$this->humpToLine($key)] = $this->convertHump((array)$item);
        } else {
            $result[$this->humpToLine($key)] = $item;
        }
    }
    return $result;
}

你可能感兴趣的:(php)