note 73703 deleted from function.json-encode by nlopess

From: Date: Sun, 11 Mar 2007 12:48:07 +0000
Subject: note 73703 deleted from function.json-encode by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-124329@lists.php.net to get a copy of this message
Note Submitter: bisqwit at iki dot fi ---- 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...

« previous php.notes (#124329) next »