How to convert all keys in a multi-dimenional array to snake_case?

前端 未结 7 2357
無奈伤痛
無奈伤痛 2021-02-14 09:08

I am trying to convert the keys of a multi-dimensional array from CamelCase to snake_case, with the added complication that some keys have an exclamation mark that I\'d like rem

7条回答
  •  滥情空心
    2021-02-14 09:34

    Although this may not be an exact answer to the question, but I wanted to post it here for people who are searching for elegant solution for changing key case in multidimensional PHP arrays. You can also adapt it for changing array keys in general. Just call a different function instead of array_change_key_case_recursive

    // converts all keys in a multidimensional array to lower or upper case
    function array_change_key_case_recursive($arr, $case=CASE_LOWER)
    {
      return array_map(function($item)use($case){
        if(is_array($item))
            $item = array_change_key_case_recursive($item, $case);
        return $item;
      },array_change_key_case($arr, $case));
    }
    

提交回复
热议问题