array with special characters to json_enconde PHP

1

Hello friends I know that this question is very common but I do not quite understand what is happening, I am trying to convert an array that contains special characters to JSON.

1.- This is the array that is returned to me from a database I am connecting to an ODBC (SQL 2000) with PDO try to set utf8 but it does not work:

Array
(
    [0] => Array ( [idOperadores] => 256 [operador] => JORGE CRUZ CEDE�O)

    [1] => Array ( [idOperadores] => 628 [operador] => BERNABE  CUEVAS BRISE�O)
)


$pdo = new PDO('odbc:odbcname', 'user', 'passwor');
$pdo->query('SET NAMES utf8');

2.- I convert the array to JSON but it did not work either:

 $result = json_encode($array,JSON_UNESCAPED_UNICODE);

Am I doing something wrong? I hope you can help me thanks.

Note: I have found some references but I would have to go through the array to set utf8_encode

    
asked by Alexander Morales 02.08.2017 в 17:25
source

1 answer

0

You have two options:

Try instantiating PDO like this (I'll give you an example with MySQL taken from here ):

$pdo = new PDO("mysql:host=127.0.0.1;dbname=datos;charset=UTF8", 'usuario', 'contraseña');

I do not think this is necessary after the previous line:

$pdo->query('SET NAMES utf8');

Or, if you are configuring everything with odbcad32.exe, then you must add the configuration of character settings (as you did with the host and the name of the DB) following the steps of this article. And there it should work as you have it:

$pdo = new PDO('odbc:odbcname', 'user', 'passwor');

Since inside your ODBC built with odbcad32.exe already has the configuration for UTF-8.

Let me know if it worked!

Greetings!

    
answered by 02.08.2017 в 18:24