入坑,项目中没按要求编码,入参、出参,出统一转换。
/** 转换 小驼峰变量到下划线形式 *如 goodsName 转换为 goods_name
* @param array $list 待转换数据 * @return array */ function convertVarToUnderline($list = []) { if (!is_array($list)) return $list; $newList = []; foreach ($list as $k => $v) { $k = strtolower(preg_replace('/[A-Z]/', '_$0', $k)); if (is_array($v)) { $newList[$k] = convertVarToUnderline($v); } else { $newList[$k] = $v; } } return $newList; } /** 转换 下划线形式 到 小驼峰变量 *如 goods_name 转换为 goodsName
* @param array $list 待转换数据 * @return array */ function convertUnderlineToLowerCamelCase($list = []) { if (!is_array($list)) return $list; $newList = []; foreach ($list as $k => $v) { $k = preg_replace_callback('/_([a-zA-Z])/', function ($match) { return strtoupper($match[1]); }, $k); if (is_array($v)) { $newList[$k] = convertUnderlineToLowerCamelCase($v); } else { $newList[$k] = $v; } } return $newList; } $arr = [ 'goodsName' => '', 'pageSize' => '', 'list' => ['goodsList' => ['timeA' => ['timeB' => ['timeC' => ['timeD' => '']]]]], ]; print_r(convertVarToUnderline($arr)); $arr = [ 'goods_name' => '', 'page_size' => '', 'list' => ['goods_list' => ['time_a' => ['time_b' => ['time_c' => ['time_d' => '']]]]], ]; print_r(convertUnderlineToLowerCamelCase($arr));