Skip to content

Instantly share code, notes, and snippets.

@katsube
Last active December 10, 2022 07:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save katsube/753b8e6eead006f7c02f61d9ae191cd8 to your computer and use it in GitHub Desktop.
Save katsube/753b8e6eead006f7c02f61d9ae191cd8 to your computer and use it in GitHub Desktop.
<?php
$apple = [
'name' => 'りんご',
'price' => 150,
'stock' => true
];
echo json_encode($apple);
<?php
$place = '{
name => \'マサラタウン\',
people => 10,
visible => true,
}';
// このままだとNULLが返ってくる
$json = json_decode($place, true);
<?php
// 文字列(JSON)を変数に代入
$monsters = '[
{"name":"スライム", "gold":10},
{"name":"ドラキー", "gold":30},
{"name":"ドラゴン", "gold":300}
]';
// 文字列(JSON)をPHPで利用できるよう変換
$data = json_decode($monsters, true);
// 謎の処理
$gold = 0;
for($i=0; $i<count($data); $i++){
printf("%sを倒した!\n", $data[$i]['name']);
$gold += $data[$i]['gold'];
}
printf("%dゴールドを手に入れた\n", $gold);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment