Json_unescaped_unicode

broken image


When handling a RESTful API request, an application usually takes the following steps that are related with response formatting: Determine various factors that may affect the response format, such as media type, language, version, etc.

In a multi-level array, JSON_FORCE_OBJECT will encode ALL nested numeric arrays as objects.
If your concern was ONLY the first-level array (e.g., to make it suitable as a MySQL JSON column), you could just cast your first-level array to object, e.g.:
$json = json_encode( (object) $array, JSON_PRESERVE_ZERO_FRACTION+JSON_UNESCAPED_UNICODE );
?>

Or, if you have large arrays and are concerned about the overhead of object casting, you could append a 'null' value beyond the size of the array, which will force the array to become associative:
$beyond = count( $array ) + 1;
if ( !
array_key_exists( $beyond, $array) )
$array[ $beyond ] = NULL;
$json = json_encode( $array, JSON_PRESERVE_ZERO_FRACTION+JSON_UNESCAPED_UNICODE );
?>

Of course, your later code has to treat an element with a 'NULL' value the same as '!isset()', if it iterates the array.
Json_unescaped_unicode
Generalmost used
  1. JSONUNESCAPEDUNICODE probably isn't actually necessary. If your version of PHP is too low, just do without it. – TRiG Nov 24 '12 at 17:14. Hmm, 5 upvotes and 'This question is unlikely to help any future visitors':) – biziclop Nov 8 '13 at 13:59. Add a comment 4.
  2. A note of caution: If you are wondering why jsonencode encodes your PHP array as a JSON object instead of a JSON array, you might want to double check your array keys because jsonencode assumes that you array is an object if your keys are not sequential.

description

Returns a string containing the JSON representation of $value.

declaration of json_encode

string json_encode ( mixed $value [, int $options ] )

test json_encode online

share json_encode

comments for json_encode

On 26. Jul 2017 11:30 fra wrote:

array short syntax doesn't work, ex. json_encode(['key' => 'value'])

On 17. May 2016 14:59 some1 wrote:

a nested array in a nested array does not work. array(array(array())) does not result in [[[]]] but in 'array(array(array()))'

On 18. Jul 2015 01:45 jacko wrote:

i can't seem to get the syntax right in my php for cleaning a text input with jason_encode prior to inserting it into the database. Can you advise?

more comments for json_encode

There are some more comments for json_encode(). To see them all click here.

Json_unescaped_unicode Online

Leave your comment





broken image