0
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?

PHPでJSONあれこれ

Last updated at Posted at 2024-12-15

概要

最近PHPを業務で触っているので、PHPでJSON扱う時の方法をまとめてみた。
JSONあれこれと言うより、配列あれこれ。

配列をJSONにしたい

第二引数JSON_UNESCAPED_UNICODEは、日本語を使用したい時に指定

<?php
    $user_group = [
                    'group_id' => 1,
                    'users' => [
                        ['name' => '佐藤', 'age' => 10],
                        ['name' => '鈴木', 'age' => 20],
                        ['name' => '高橋', 'age' => 30]
                    ]
    ];
    var_export(json_encode($user_group, JSON_UNESCAPED_UNICODE));
?>
出力
'{"group_id":1,"users":[{"name":"佐藤","age":10},{"name":"鈴木","age":20},{"name":"高橋","age":30}]}'

JSONを配列にしたい

第二引数がtrueの場合、連想配列。falseの場合、オブジェクト。

<?php
    $user_group = [
                    'group_id' => 1,
                    'users' => [
                        ['name' => '佐藤', 'age' => 10],
                        ['name' => '鈴木', 'age' => 20],
                        ['name' => '高橋', 'age' => 30]
                    ]
    ];
    $user_group_json = json_encode($user_group, JSON_UNESCAPED_UNICODE);
    var_export(json_decode($user_group_json, true));
?>
出力
array (
  'group_id' => 1,
  'users' => 
  array (
    0 => 
    array (
      'name' => '佐藤',
      'age' => 10,
    ),
    1 => 
    array (
      'name' => '鈴木',
      'age' => 20,
    ),
    2 => 
    array (
      'name' => '高橋',
      'age' => 30,
    ),
  ),
)

基本は配列に変換してあれこれする

配列の中を取り出す

<?php
    $user_group = [
                    'group_id' => 1,
                    'users' => [
                        ['name' => '佐藤', 'age' => 10],
                        ['name' => '鈴木', 'age' => 20],
                        ['name' => '高橋', 'age' => 30]
                    ]
    ];
    $user_group_json = json_encode($user_group, JSON_UNESCAPED_UNICODE);
    $user_group_array = json_decode($user_group_json, true);
    
    echo('group_id:'.$user_group_array['group_id']."\n");
    foreach($user_group_array['users'] as $user) {
        echo('name:'.$user['name'].'、');
        echo('age:'.$user['age']."\n");
    }
?>
出力
group_id:1
name:佐藤、age:10
name:鈴木、age:20
name:高橋、age:30

フィルターをかけたい

array_valuesでインデックスを振り直すのを忘れずに

<?php
    $user_group = [
                    'group_id' => 1,
                    'users' => [
                        ['name' => '佐藤', 'age' => 10],
                        ['name' => '鈴木', 'age' => 20],
                        ['name' => '高橋', 'age' => 30]
                    ]
    ];
    $user_group_json = json_encode($user_group, JSON_UNESCAPED_UNICODE);
    $user_group_array = json_decode($user_group_json, true);
    
    $user_group_array['users'] = array_values(array_filter($user_group_array['users'], fn($user) => $user['name'] === '鈴木'));
    var_export($user_group_array);
?>
出力
array (
  'group_id' => 1,
  'users' => 
  array (
    0 => 
    array (
      'name' => '鈴木',
      'age' => 20,
    ),
  ),
)

ユーザを追加したい

<?php
    $user_group = [
                    'group_id' => 1,
                    'users' => [
                        ['name' => '佐藤', 'age' => 10],
                        ['name' => '鈴木', 'age' => 20],
                        ['name' => '高橋', 'age' => 30]
                    ]
    ];
    $user_group_json = json_encode($user_group, JSON_UNESCAPED_UNICODE);
    $user_group_array = json_decode($user_group_json, true);
    
    $add_user = ['name' => '田中', 'age' => 40];
    array_push($user_group_array['users'], $add_user);
    var_export($user_group_array);
?>
出力
array (
  'group_id' => 1,
  'users' => 
  array (
    0 => 
    array (
      'name' => '佐藤',
      'age' => 10,
    ),
    1 => 
    array (
      'name' => '鈴木',
      'age' => 20,
    ),
    2 => 
    array (
      'name' => '高橋',
      'age' => 30,
    ),
    3 => 
    array (
      'name' => '田中',
      'age' => 40,
    ),
  ),
)

keyを追加したい

<?php
    $user_group = [
                    'group_id' => 1,
                    'users' => [
                        ['name' => '佐藤', 'age' => 10],
                        ['name' => '鈴木', 'age' => 20],
                        ['name' => '高橋', 'age' => 30]
                    ]
    ];
    $user_group_json = json_encode($user_group, JSON_UNESCAPED_UNICODE);
    $user_group_array = json_decode($user_group_json, true);
    
    $add_user_group_key = ['group_memo' => 'メモ'];
    $user_group_array = array_merge($user_group_array, $add_user_group_key);
    var_export($user_group_array);
?>
出力
array (
  'group_id' => 1,
  'users' => 
  array (
    0 => 
    array (
      'name' => '佐藤',
      'age' => 10,
    ),
    1 => 
    array (
      'name' => '鈴木',
      'age' => 20,
    ),
    2 => 
    array (
      'name' => '高橋',
      'age' => 30,
    ),
  ),
  'group_memo' => 'メモ',
)

終わり

配列操作系の関数は他にもありますが、一旦終わります。

0
1
0

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
0
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?