Sobes.tech
Junior — Middle

How to convert a data array into JSON format in your programming language?

sobes.tech AI

Answer from AI

In PHP, the function json_encode() is used to convert an array of data into JSON format. It takes an array or object and returns a JSON-formatted string.

Example:

$data = [
    "name" => "Ivan",
    "age" => 30,
    "city" => "Moscow"
];

$json = json_encode($data, JSON_UNESCAPED_UNICODE);
echo $json; // Outputs: {"name":"Ivan","age":30,"city":"Moscow"}

The JSON_UNESCAPED_UNICODE option allows Russian characters to be preserved in a readable form, rather than as Unicode-escaped sequences.