I am referring to this question here on stackoverflow, and I have the same problem, except that I use postgre database and can't seem to get this working.
Th开发者_开发知识库is is my php function which is querying the database:
public function getCashData($id, $date)
{
if ($this->openConnection()){
$query = "SELECT * FROM cash_register (". $id .", '". $date ."');";
$result = pg_query($query);
if (!$result){
return false;
}
return pg_fetch_all($result);
}
}
I call this function like this:
$cashReport = getCashReport($id, $date);
$cashReport = array_map('utf8_encode' , $casaReport); //**note: please read below
echo json_encode($casaReport);
**This was working perfectly when I was returning only one row of result (and not all like now), but now when I'm returning an array of rows this array_map function (which I found in the above mentioned link) is not working as it expects an array and not an array of arrays.
Can you guys help me solve this problem?
Try this:
function encode_items(&$item, $key)
{
$item = utf8_encode($item);
}
array_walk_recursive($cashReport, 'encode_items');
It's also possible to "tell" the postgre server to send the data utf-8 encoded.
see http://docs.php.net/pg_set_client_encoding
I did this in the end as I was forced to "fix" it asap:
$json = json_encode($cashReport);
$jsonEscapedNulls = str_replace('null', '""', $json);
echo $jsonEscapedNulls;
Now, I understand that "fixes" aren't solutions but I had no other choice in a give period of time :(. So if someone gives an answer which will work not as a fix but as a general solution I will be grateful. Until then, this will have to do.
精彩评论