Obsah
json_encode
Funkce json_encode() pro verze PHP < 5.2.0
| Autor | David Grudl |
| Licence | public domain |
Kód funkce
if (!function_exists('json_encode')) {
/**
* JSON encode
*
* @author David Grudl
* @copyright Copyright (c) 2007 David Grudl
*/
function json_encode($val)
{
// indexed array
if (is_array($val) && (!$val
|| array_keys($val) === range(0, count($val) - 1))) {
return '[' . implode(',', array_map('json_encode', $val)) . ']';
}
// associative array
if (is_array($val) || is_object($val)) {
$tmp = array();
foreach ($val as $k => $v) {
$tmp[] = json_encode((string) $k) . ':' . json_encode($v);
}
return '{' . implode(',', $tmp) . '}';
}
if (is_string($val)) {
$val = str_replace(array("\\", "\x00"), array("\\\\", "\\u0000"), $val); // due to bug #40915
return '"' . addcslashes($val, "\x8\x9\xA\xC\xD/\"") . '"';
}
if (is_int($val) || is_float($val)) {
return rtrim(rtrim(number_format($val, 5, '.', ''), '0'), '.');
}
if (is_bool($val)) {
return $val ? 'true' : 'false';
}
return 'null';
}
}




paranoiq | 29. 7. 2010, 15:34 | comment
number_format($val, 5, '.', '')proč jen 5 míst za tečkou?