note 73703 added to function.json-encode
| From: | bisqwit at iki dot fi | Date: | Wed, 07 Mar 2007 10:47:38 +0000 |
| Subject: | note 73703 added to function.json-encode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-124188@lists.php.net to get a copy of this message | ||
Here's a slightly simpler binary-safe version of json_encode. Note that associative array keys
are converted to strings to comply with the official json specification. (Arrays with all-numeric
keys in a pure sequence are converted to lists instead.)
function myjson($s)
{
if(is_numeric($s)) return $s;
if(is_string($s)) return utf8_decode(json_encode(utf8_encode($s)));
if($s===false) return 'false';
if($s===true) return 'true';
if(is_array($s))
{
$c=0;
foreach($s as $k=>&$v)
if($k !== $c++)
{
foreach($s as $k=>&$v) $v = myjson((string)$k).':'.myjson($v);
return '{'.join(',', $s).'}';
}
return '[' . join(',', array_map('myjson', $s)) . ']';
}
return 'null';
}
Of course it'd be nice if this hack wasn't required in the first place...
----
Server IP: 80.81.171.181
Probable Submitter: 194.157.58.35
----
Manual Page -- http://www.php.net/manual/en/function.json-encode.php
Edit -- https://master.php.net/note/edit/73703
Del: integrated -- https://master.php.net/note/delete/73703/integrated
Del: useless -- https://master.php.net/note/delete/73703/useless
Del: bad code -- https://master.php.net/note/delete/73703/bad+code
Del: spam -- https://master.php.net/note/delete/73703/spam
Del: non-english -- https://master.php.net/note/delete/73703/non-english
Del: in docs -- https://master.php.net/note/delete/73703/in+docs
Del: other reasons-- https://master.php.net/note/delete/73703
Reject -- https://master.php.net/note/reject/73703
Search -- https://master.php.net/manage/user-notes.php