Back to questions
JuniorTheoryCommon
What does the program print?
<?php
$data = ['id' => 1, 'tags' => ['a', 'b'], 'ok' => true];
$json = json_encode($data);
$back = json_decode($json, true);
$obj = json_decode($json);
echo $json, ' ', $back['tags'][1], ' ', $obj->id, "\n";{"id":1,"tags":["a","b"],"ok":true} b 1
- Expecting
json_decodeto return an array every time: without the second argumenttrueit returns astdClassobject, read with->, not with[]. - Expecting every PHP array to become a JSON object: a list with keys 0, 1, 2… is written as a JSON array, and only other keys give an object.
- What does
json_decodereturn for a string that is not valid JSON? - Which flag makes
json_encodewrite Cyrillic letters as they are?