note 105923 modified in function.json-encode by danbrown
| From: | danbrown@php.net | Date: | Mon, 26 Sep 2011 14:52:58 +0000 |
| Subject: | note 105923 modified in function.json-encode by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-183442@lists.php.net to get a copy of this message | ||
If, for some reason you need to force a single object to be an array, you can use array_values() --
this can be necessary if you have an array with only one entry, as json_encode will assign it as an
object otherwise :
<?php
$object[0] = array("foo" => "bar", 12 => true);
$encoded_object = json_encode($object);
?>
output:
{"1": {"foo": "bar", "12": "true"}}
<?php $encoded = json_encode(array_values($object)); ?>
output:
[{"foo": "bar", "12": "true"}]
--was--
If, for some reason you need to force a single object to be an array, you can use array_values() --
this can be necessary if you have an array with only one entry, as json_encode will assign it as an
object otherwise :
$object[0] = array("foo" => "bar", 12 => true);
$encoded_object = json_encode($object);
output:
{"1": {"foo": "bar", "12": "true"}}
$encoded = json_encode(array_values($object));
output:
[{"foo": "bar", "12": "true"}]
http://php.net/manual/en/function.json-encode.php