4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

PHPの配列のkeyをスネーク⇔キャメル変換する関数を作ってみた

Posted at

コピペ用関数

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' => 'aiueo@gmail.com',
    'children' => [
        [
            'userId' => '11',
            'nickName' => 'piyo',
        ],
        [
            'userId' => '12',
            'nickName' => 'ponyo',
        ],
    ]
];

$snakelized_array = convertArrayKeyNamesIntoSnakeCase($array);

// $snakelized_array = [
//     'user_id' => '1',
//     'nick_name' => 'nick',
//     'mail_address' => 'aiueo@gmail.com',
//     'children' => [
//         [
//             'user_id' => '11',
//             'nick_name' => 'piyo',
//         ],
//         [
//             'user_id' => '12',
//             'nick_name' => 'ponyo',
//         ],
//     ]
// ];

camelize.php
$array = [
    'user_id' => '1',
    'nick_name' => 'nick',
    'mail_address' => 'aiueo@gmail.com',
    'children' => [
        [
            'user_id' => '11',
            'nick_name' => 'piyo',
        ],
        [
            'user_id' => '12',
            'nick_name' => 'ponyo',
        ],
    ]
];

$camelized_array = convertArrayKeyNamesIntoCamelCase($array);

// $camelized_array = [
//     'userId' => '1',
//     'nickName' => 'nick',
//     'mailAddress' => 'aiueo@gmail.com',
//     'children' => [
//         [
//             'userId' => '11',
//             'nickName' => 'piyo',
//         ],
//         [
//             'userId' => '12',
//             'nickName' => 'ponyo',
//         ],
//     ]
// ];

Javascript(Vue.jsなど)では基本的にキャメルケース、
PHP(Laravelなど)では基本的にスネークケースが使用されているので、
その2つの言語を使って開発する時に重宝します。

ちなみにLaravelではこの関数をファサードにして、
いろんなところで使っています。

ぜひお試しあれ。

4
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?