Last active
December 10, 2022 07:04
-
-
Save katsube/753b8e6eead006f7c02f61d9ae191cd8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$apple = [ | |
'name' => 'りんご', | |
'price' => 150, | |
'stock' => true | |
]; | |
echo json_encode($apple); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$place = '{ | |
name => \'マサラタウン\', | |
people => 10, | |
visible => true, | |
}'; | |
// このままだとNULLが返ってくる | |
$json = json_decode($place, true); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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