json_encode
函数用于将 PHP 数据结构转换为 JSON 字符串。json_decode
函数用于将 JSON 字符串转换为 PHP 数据结构。
// 将 PHP 数据结构转换为 JSON 字符串
$data = ["name" => "John","age" => 25,"city" => "New York"
];$jsonString = json_encode($data);
echo $jsonString; // 输出:{"name":"John","age":25,"city":"New York"}// 将 JSON 字符串转换为 PHP 数据结构
$jsonString = '{"name":"John","age":25,"city":"New York"}';$data = json_decode($jsonString, true);
var_dump($data);
/*
输出:
array(3) {["name"]=>string(4) "John"["age"]=>int(25)["city"]=>string(8) "New York"
}
*/