🤖 Ai回答: PHP provides built-in functions to work with JSON (JavaScript Object Notation), a lightweight data interchange format、Here are the most commonly used PHP functions for handling JSON:
🔹 1、`json_encode()` – Convert PHP data to JSON
Converts a PHP variable (array, object, etc.) into a JSON string.
// Encode to JSON
$json = json_encode($user, JSON_PRETTY_PRINT);
echo $json;
// Decode back
$decoded = json_decode($json, true);
var_dump($decoded);
?
✅ Tips
Use `json_decode(..., true)` when you want arrays instead of objects.
Always validate JSON using `json_last_error()` after operations.
JSON only supports basic data types: strings, numbers, booleans, arrays, objects, and `null`.
Let me know if you want examples for files, APIs, or working with nested structures!