PHPの配列のkeyをスネーク⇔キャメル変換する関数を作ってみた
コピペ用関数
functions.php
//キャメル→スネーク変換
function convertStringIntoSnakeCase(string $string): string
{
return ltrim(strtolower(preg_replace('/[A-Z]/', '_\0', $string)), '_');
}
//配列のkeyを全てキャメル→スネーク変換
function convertArrayKeyNamesIntoSnakeCase($array): array
{
$snakelized_array = [];
foreach ($array as $key => $value) {
if (
is_array($value) === true ||
is_object($value) === true
) {
if (is_numeric($value) === true) {
$snakelized_array[] = convertArrayKeyNamesIntoSnakeCase($value);
} else {
$snakelized_key_name = convertStringIntoSnakeCase($key);
$snakelized_array[$snakelized_key_name] = convertArrayKeyNamesIntoSnakeCase($value);
}
} else {
$snakelized_key_name = convertStringIntoSnakeCase($key);
$snakelized_array[$snakelized_key_name] = $value;
}
}
return $snakelized_array;
}
//スネーク→キャメル変換
function convertStringIntoCamelCase(string $string): string
{
return lcfirst(strtr(ucwords(strtr($string, ['_' => ' '])), [' ' => '']));
}
//配列のkeyを全てスネーク→キャメル変換
function convertArrayKeyNamesIntoCamelCase($array): array
{
$camelized_array = [];
foreach ($array as $key => $value) {
if (
is_array($value) === true ||
is_object($value) === true
) {
if (is_numeric($value) === true) {
$camelized_array[] = convertArrayKeyNamesIntoCamelCase($value);
} else {
$camelized_key_name = convertStringIntoCamelCase($key);
$camelized_array[$camelized_key_name] = convertArrayKeyNamesIntoCamelCase($value);
}
} else {
$camelized_key_name = convertStringIntoCamelCase($key);
$camelized_array[$camelized_key_name] = $value;
}
}
return $camelized_array;
}
使い方
snakelize.php
$array = [
'userId' => '1',
'nickName' => 'nick',
'mailAddress' => '[email protected]',
'children' => [
[
'userId' => '11',
'nickName' => 'piyo',
],
[
'userId' => '12',
'nickName' => 'ponyo',
],
]
];
$snakelized_array = convertArrayKeyNamesIntoSnakeCase($array);
// $snakelized_array = [
// 'user_id' => '1',
// 'nick_name' => 'nick',
// 'mail_address' => '[email protected]',
// 'children' => [
// [
// 'user_id' => '11',
// 'nick_name' => 'piyo',
// ],
// [
// 'user_id' => '12',
// 'nick_name' => 'ponyo',
// ],
// ]
// ];
camelize.php
$array = [
'user_id' => '1',
'nick_name' => 'nick',
'mail_address' => '[email protected]',
'children' => [
[
'user_id' => '11',
'nick_name' => 'piyo',
],
[
'user_id' => '12',
'nick_name' => 'ponyo',
],
]
];
$camelized_array = convertArrayKeyNamesIntoCamelCase($array);
// $camelized_array = [
// 'userId' => '1',
// 'nickName' => 'nick',
// 'mailAddress' => '[email protected]',
// 'children' => [
// [
// 'userId' => '11',
// 'nickName' => 'piyo',
// ],
// [
// 'userId' => '12',
// 'nickName' => 'ponyo',
// ],
// ]
// ];
functions.php
//キャメル→スネーク変換
function convertStringIntoSnakeCase(string $string): string
{
return ltrim(strtolower(preg_replace('/[A-Z]/', '_\0', $string)), '_');
}
//配列のkeyを全てキャメル→スネーク変換
function convertArrayKeyNamesIntoSnakeCase($array): array
{
$snakelized_array = [];
foreach ($array as $key => $value) {
if (
is_array($value) === true ||
is_object($value) === true
) {
if (is_numeric($value) === true) {
$snakelized_array[] = convertArrayKeyNamesIntoSnakeCase($value);
} else {
$snakelized_key_name = convertStringIntoSnakeCase($key);
$snakelized_array[$snakelized_key_name] = convertArrayKeyNamesIntoSnakeCase($value);
}
} else {
$snakelized_key_name = convertStringIntoSnakeCase($key);
$snakelized_array[$snakelized_key_name] = $value;
}
}
return $snakelized_array;
}
//スネーク→キャメル変換
function convertStringIntoCamelCase(string $string): string
{
return lcfirst(strtr(ucwords(strtr($string, ['_' => ' '])), [' ' => '']));
}
//配列のkeyを全てスネーク→キャメル変換
function convertArrayKeyNamesIntoCamelCase($array): array
{
$camelized_array = [];
foreach ($array as $key => $value) {
if (
is_array($value) === true ||
is_object($value) === true
) {
if (is_numeric($value) === true) {
$camelized_array[] = convertArrayKeyNamesIntoCamelCase($value);
} else {
$camelized_key_name = convertStringIntoCamelCase($key);
$camelized_array[$camelized_key_name] = convertArrayKeyNamesIntoCamelCase($value);
}
} else {
$camelized_key_name = convertStringIntoCamelCase($key);
$camelized_array[$camelized_key_name] = $value;
}
}
return $camelized_array;
}
snakelize.php
$array = [
'userId' => '1',
'nickName' => 'nick',
'mailAddress' => '[email protected]',
'children' => [
[
'userId' => '11',
'nickName' => 'piyo',
],
[
'userId' => '12',
'nickName' => 'ponyo',
],
]
];
$snakelized_array = convertArrayKeyNamesIntoSnakeCase($array);
// $snakelized_array = [
// 'user_id' => '1',
// 'nick_name' => 'nick',
// 'mail_address' => '[email protected]',
// 'children' => [
// [
// 'user_id' => '11',
// 'nick_name' => 'piyo',
// ],
// [
// 'user_id' => '12',
// 'nick_name' => 'ponyo',
// ],
// ]
// ];
camelize.php
$array = [
'user_id' => '1',
'nick_name' => 'nick',
'mail_address' => '[email protected]',
'children' => [
[
'user_id' => '11',
'nick_name' => 'piyo',
],
[
'user_id' => '12',
'nick_name' => 'ponyo',
],
]
];
$camelized_array = convertArrayKeyNamesIntoCamelCase($array);
// $camelized_array = [
// 'userId' => '1',
// 'nickName' => 'nick',
// 'mailAddress' => '[email protected]',
// 'children' => [
// [
// 'userId' => '11',
// 'nickName' => 'piyo',
// ],
// [
// 'userId' => '12',
// 'nickName' => 'ponyo',
// ],
// ]
// ];
Javascript(Vue.jsなど)では基本的にキャメルケース、
PHP(Laravelなど)では基本的にスネークケースが使用されているので、
その2つの言語を使って開発する時に重宝します。
ちなみにLaravelではこの関数をファサードにして、
いろんなところで使っています。
ぜひお試しあれ。
Author And Source
この問題について(PHPの配列のkeyをスネーク⇔キャメル変換する関数を作ってみた), 我々は、より多くの情報をここで見つけました https://qiita.com/yusuke5/items/485b3edc2e2670f2b12c著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .