note 106863 deleted from function.json-encode by danbrown
| From: | danbrown@php.net | Date: | Tue, 03 Jan 2012 22:14:24 +0000 |
| Subject: | note 106863 deleted from function.json-encode by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-185339@lists.php.net to get a copy of this message | ||
Note Submitter: trevalayan at hotmail dot com
----
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))"