note 93860 deleted from function.json-encode by derick

From: Date: Fri, 28 May 2010 16:33:02 +0000
Subject: note 93860 deleted from function.json-encode by derick
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-169696@lists.php.net to get a copy of this message
Note Submitter: Sergey Aikinkulov ---- <?php /* Convert PHP array to JSON -- Using: - on server side: echo(arr2json([PHP array])) - on client side(JavaScript): var obj = eval('(' + [XMLHTTPRequest.responseText] + ')'); */ function arr2json($arr){ foreach($arr as $k=>&$val) $json[] = $k.':'.php2js($val); if(count($json) > 0) return '{'.implode(',', $json).'}'; else return ''; } function php2js($val){ if(is_array($val)) return arr2json($val); if(is_string($val)) return '"'.addslashes($val).'"'; if(is_bool($val)) return 'Boolean('.(int) $val.')'; if(is_null($val)) return '""'; return $val; } ?>

« previous php.notes (#169696) next »