递归地将多维数组中的所有键转换为snake_case
我正在尝试将多维数组的键从camelCase或StudlyCase/PascalCase转换为snake_case,但增加了一些复杂性,即某些键有一个我想删除的前导感叹号。
例如:
$array = array(
'!AccountNumber' => '00000000',
'Address' => array(
'!Line1' => '10 High Street',
'!line2' => 'London'));
我想转换为:
$array = array(
'account_number' => '00000000',
'address' => array(
'line1' => '10 High Street',
'line2' => 'London'));
我现实生活中的数组很大,而且有很多层次。
I am trying to convert the keys of a multi-dimensional array from camelCase or StudlyCase/PascalCase to snake_case, with the added complication that some keys have a leading exclamation mark that I'd like to be removed.
For example:
$array = array(
'!AccountNumber' => '00000000',
'Address' => array(
'!Line1' => '10 High Street',
'!line2' => 'London'));
I would like to convert to:
$array = array(
'account_number' => '00000000',
'address' => array(
'line1' => '10 High Street',
'line2' => 'London'));
My real-life array is huge and goes many levels deep.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
这是我使用的修改后的函数,取自灵魂合并的响应:
This is the modified function I have used, taken from soulmerge's response:
这是亚伦的更通用的版本。这样你就可以为所有按键插入你想要操作的功能。我假设了一个静态类。
Here's a more generalized version of Aaron's. This way you can just plug the function you want to be operated on for all keys. I assumed a static class.
您可以在数组键上运行 foreach,这样您就可以就地重命名键:
You can run a foreach on the arrays keys, this way you'll rename the keys in-place:
虽然这可能不是问题的准确答案,但我想将其发布在这里,供那些正在寻找优雅的解决方案来更改多维 PHP 数组中的关键案例的人们使用。您还可以调整它以更改一般的数组键。只需调用不同的函数而不是 array_change_key_case_recursive
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
创建一个像这样的函数:
并像这样调用它:
对于工作示例请参阅此
Create a function like:
and call this like:
for working example see this
我想说,您必须编写一个函数来复制数组(一级),并在任何值是数组(递归函数)时让该函数调用自身。
您到底需要什么具体帮助?
I'd say you would have to write a function to copy the array (one level) and have that function call itself if any of the values is an array (a recursive function).
What exactly do you need any specific help with?
为了避免改变原始数组并返回具有重新格式化键的新数组,请使用递归函数而不通过引用进行修改,并在每个级别上返回新数组。
代码:(演示)
或在不生成新数组的情况下改变每个级别:(Demo)
测试数据:
输出:
即使使用引用修改(如 aaronrussell 的答案),foreach 循环仍在创建数据副本并且仍然必须生成新密钥并销毁旧密钥 - 没有就地修改密钥的快捷方式。
To avoid mutating the original array and return a new array with reformatted keys, use a recursive function without modifying by reference and return the new array on every level.
Code: (Demo)
Or mutate each level in place without generating a new array: (Demo)
Test data:
Output:
Even if modification by reference is used (like in aaronrussell's answer), the foreach loop is still creating copies of data and new keys still have to be generated and old keys destroyed -- there is no shortcut to modify keys in-place.