Question

I have result array as below

$array = array(array('nome'=>'Paição','cidade'=>'São Paulo'),array('nome'=>'Paição','cidade'=>'São Paulo'));

when i encode same array with json_encode in php it not return as same as array and it return as below json string

$json = json_encode($array);
$result = [{"nome":"Pai\u00e7\u00e3o","cidade":"S\u00e3o Paulo"},{"nome":"Pai\u00e7\u00e3o","cidade":"S\u00e3o Paulo"}]

Please anyone have idea to convert this array as same characters in php

Thanks,

Était-ce utile?

La solution 2

I found solution for who use below 5.4 php version,

$arr = array(array('nome'=>'Paição','cidade'=>'São Paulo'),array('nome'=>'Paição','cidade'=>'São Paulo'));

$array = preg_replace("/\\\\u([a-f0-9]{4})/e", "iconv('UCS-4LE','UTF-8',pack('V', hexdec('U$1')))", json_encode($arr));

Result of above is same as passed value

[{"nome":"Paição","cidade":"São Paulo"},{"nome":"Paição","cidade":"São Paulo"}]

and you have to also set mysql_query("SET NAMES utf8"); after mysql connection.

Autres conseils

Use JSON_UNESCAPED_SLASHES in the json_encode.

Do like this

<?php
$array = array(array('nome'=>'Paição','cidade'=>'São Paulo'),array('nome'=>'Paição','cidade'=>'São Paulo'));
$json = json_encode($array,JSON_UNESCAPED_UNICODE);
echo $json;

Try using this function wrapping the whole json_encode():

utf8_encode(json_encode());

On the other hand, if what you're doing is sending and parsing data from mysql you should define the connection to utf8:

$conn=  mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
mysqli_set_charset($conn,'utf8');
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top