Encoding UTF-8 characters is problematic with the json_encode() function. After this function encodes values from LATIN1 to UTF-8, the output becomes null.
How can json_encode() be used to encode UTF-8 values correctly, without resorting to third-party libraries like Zend Framework?
To resolve this issue, follow these steps:
Here's an example code snippet:
<code class="php">// Create an empty array for the encoded resultset $rows = array(); // Loop over the db resultset and put encoded values into $rows while($row = mysql_fetch_assoc($result)) { $rows[] = array_map('utf8_encode', $row); } // Output $rows echo json_encode($rows);</code>
The above is the detailed content of How to Encode UTF-8 Characters Correctly with json_encode() in PHP?. For more information, please follow other related articles on the PHP Chinese website!