概要
最近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' => 'メモ',
)
終わり
配列操作系の関数は他にもありますが、一旦終わります。