CKTIS / PHP
json_encode()
Serialize a PHP value as UTF-8 JSON.
Version: PHP 8.4; repository runtime, production runtime not attested
Syntax
json_encode(mixed $value, int $flags = 0, int $depth = 512): string|falseSerialize a PHP value as UTF-8 JSON. An array with contiguous keys from zero becomes a JSON array, whereas a gap produces an object with textual keys. Invalid UTF-8 can return false or throw with JSON_THROW_ON_ERROR. Set a suitable nesting depth for structured input. Under the default flags the iteration order of integer keys must be 0,1,..., not just the same set of keys. Keys in order 1,0 produce an object.
Signature
json_encode(mixed $value, int $flags = 0, int $depth = 512): string|false
Example output
[["a","b"],{"0":"a","2":"b"}]
true
JsonException
{"1":"one","0":"zero"}
Examples
<?php
echo json_encode([["a","b"],[0=>"a",2=>"b"]]), PHP_EOL;
echo json_encode(json_encode("\xFF") === false), PHP_EOL;
try { json_encode("\xFF",JSON_THROW_ON_ERROR); } catch(JsonException $e) { echo get_class($e); }
echo PHP_EOL,json_encode([1=>"one",0=>"zero"]);
Result
[["a","b"],{"0":"a","2":"b"}]
true
JsonException
{"1":"one","0":"zero"}