note 106863 added to function.json-encode
| From: | trevalayan at hotmail dot com | Date: | Tue, 13 Dec 2011 15:44:24 +0000 |
| Subject: | note 106863 added to function.json-encode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-185064@lists.php.net to get a copy of this message | ||
me.pierB offered a solution for dealing with UTF8 characters in json_encode. Unfortunately there is
a minor bug so here is the complete code with the bugfix:
"Combining last 2 comments, json_decode and json_encode can gain great advantages from
following:"
<?php
function array_ut8_encode_recursive(&$itm) {
$new = $itm;
if (is_string($new)) return utf8_encode($new);
if (is_array($new)) return array_map('array_ut8_encode_recursive', $new);
if (is_object($new)) foreach (get_object_vars($new) as $key => $value ) $new->$key =
array_ut8_encode_recursive($new->$key);
return $new;
} // array_ut8_encode_recursive()
function array_ut8_decode_recursive(&$itm) {
$new = $itm;
if (is_string($new)) return utf8_decode($new);
if (is_array($new)) return array_map('array_ut8_decode_recursive', $new);
if (is_object($new)) foreach (get_object_vars($new) as $key => $value ) $new->$key =
array_ut8_decode_recursive($new->$key);
return $new;
} // array_ut8_decode_recursive()
?>
"json_encode(array_ut8_encode_recursive($data))"
----
Server IP: 92.48.74.199
Probable Submitter: 94.195.193.186
----
Manual Page -- http://www.php.net/manual/en/function.json-encode.php
Edit -- https://master.php.net/note/edit/106863
Del: integrated -- https://master.php.net/note/delete/106863/integrated
Del: useless -- https://master.php.net/note/delete/106863/useless
Del: bad code -- https://master.php.net/note/delete/106863/bad+code
Del: spam -- https://master.php.net/note/delete/106863/spam
Del: non-english -- https://master.php.net/note/delete/106863/non-english
Del: in docs -- https://master.php.net/note/delete/106863/in+docs
Del: other reasons-- https://master.php.net/note/delete/106863
Reject -- https://master.php.net/note/reject/106863
Search -- https://master.php.net/manage/user-notes.php