Maison >développement back-end >tutoriel php >Comment empêcher les caractères spéciaux de se convertir en null lors de l'utilisation de json_encode ?
json_encode function: Converting Special Characters to Null
When encoding an array using json_encode, special characters present in array elements may be converted to empty strings, resulting in data loss. This occurs when the array contains the copyright, trademark, or other special symbols within strings.
Solution:
The json_encode documentation specifies that all string data must be UTF-8 encoded. To resolve this issue, apply utf8_encode() to your array before encoding:
$arr = array_map('utf8_encode', $arr); $json = json_encode($arr);
This encodes the special characters in the array, preventing their conversion to empty strings. For example:
$arr = array ( "funds" => "ComStage STOXX®Europe 600 Techn NR ETF", "time"=>....); $json = json_encode(array_map('utf8_encode', $arr));
This will result in the correct JSON output:
{"funds":"ComStage STOXX\u00c2\u00aeEurope 600 Techn NR ETF"}
Additional Considerations:
References:
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!