$data = array("name" => "John", "age" => 30, "city" => "New York");
$json_data = json_encode($data);
echo $json_data;
$json_data = '{"name": "John", "age": 30, "city": "New York"}';
$data = json_decode($json_data, true);
print_r($data);
$json_data = '{"name": "John", "age": 30, "address": {"street": "123 Main St", "city": "New York"}}';
$data = json_decode($json_data, true);
echo $data['address']['city'];
$json_data = '{"name": "John", "age": 30, "hobbies": ["reading", "running", "cooking"]}';
$data = json_decode($json_data, true);
foreach ($data['hobbies'] as $hobby) {
echo $hobby . "<br>";
}
$json_data = '{"date": "2022-01-01"}';
$data = json_decode($json_data, true);
$date = date("Y-m-d", strtotime($data['date']));
echo $date;
這些是一些常見的實戰技巧,可以幫助您更好地處理和操作JSON數據。