note 99837 modified in function.json-encode by danbrown
| From: | danbrown@php.net | Date: | Thu, 09 Sep 2010 15:54:30 +0000 |
| Subject: | note 99837 modified in function.json-encode by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-172429@lists.php.net to get a copy of this message | ||
This may help others who are seeing null strings returned by json_encode().
This function will encode all array values to utf8 so they are safe for json_encode();
usage:
<?php
json_encode(utf8json($dataArray));
function utf8json($inArray) {
static $depth = 0;
/* our return object */
$newArray = array();
/* safety recursion limit */
$depth ++;
if($depth >= '30') {
return false;
}
/* step through inArray */
foreach($inArray as $key=>$val) {
if(is_array($val)) {
/* recurse on array elements */
$newArray[$key] = utf8json($inArray);
} else {
/* encode string values */
$newArray[$key] = utf8_encode($val);
}
}
/* return utf8 encoded array */
return $newArray;
}
?>
--was--
This may help others who are seeing null strings returned by json_encode().
This function will encode all array values to utf8 so they are safe for json_encode();
usage:
json_encode(utf8json($dataArray));
function utf8json($inArray) {
static $depth = 0;
/* our return object */
$newArray = array();
/* safety recursion limit */
$depth ++;
if($depth >= '30') {
return false;
}
/* step through inArray */
foreach($inArray as $key=>$val) {
if(is_array($val)) {
/* recurse on array elements */
$newArray[$key] = utf8json($inArray);
} else {
/* encode string values */
$newArray[$key] = utf8_encode($val);
}
}
/* return utf8 encoded array */
return $newArray;
}
http://php.net/manual/en/function.json-encode.php