Output in json with json_encode ()

5
  

json_encode () - Returns the JSON representation of a value

If I do:

$foo = array('a', 'b', 'c', 'd', 'e');
echo json_encode($foo);

I'll get:

["a","b","c","d","e"]

So far so good, I'm getting an array and turning it into a json object.

Now if I try to query and return a multidimensional array to transform into json it does not work.

Example:

include '../db/pdo.php';
$sql = $pdo->prepare('SELECT  * FROM CLIENTES WHERE ID > 0');
$sql->execute();
$result = $sql->fetchAll();
echo json_encode($result);

What am I forgetting? Both are getting an array and transforming into JSON.

Array output:

array (size=2)
  0 => 
    array (size=22)
      'CODIGO' => string '1' (length=1)
      'NOME' => string 'MARIA DA SILVA' (length=14)
      'TIPOPESSOA' => string 'F' (length=1)
      'CPF' => string '12345678912' (length=11)
      'CGC' => null
      'CONTACORRENTE' => null
      'REGIAO' => null
      'ENDERECO' => null
      'NUMERO' => null
      'SETOR' => null
      'CIDADE' => null
      'UF' => null
      'CEP' => null
      'FONE' => null
      'FAXCEL' => null
      'ENDENDERECO_1' => null
      'NUMERO_1' => null
      'SETOR_1' => null
      'CIDADE_1' => null
      'UF_1' => null
      'CEP_1' => null
      'FONE_1' => null
  1 => 
    array (size=22)
      'CODIGO' => string '2' (length=1)
      'NOME' => string 'SU�LLEM DA SILVA' (length=24)
      'TIPOPESSOA' => string 'F' (length=1)
      'CPF' => string '12345678912' (length=11)
      'CGC' => null
      'CONTACORRENTE' => null
      'REGIAO' => null
      'ENDERECO' => string 'ENDERECO' (length=19)
      'NUMERO' => string '401' (length=3)
      'SETOR' => string 'SETOR' (length=8)
      'CIDADE' => string 'BUENO' (length=13)
      'UF' => string 'RO' (length=2)
      'CEP' => string '12345678912' (length=8)
      'FONE' => string '12345678912' (length=10)
      'FAXCEL' => null
      'ENDENDERECO_1' => null
      'NUMERO_1' => null
      'SETOR_1' => null
      'CIDADE_1' => null
      'UF_1' => null
      'CEP_1' => null
      'FONE_1' => null

Using json_last_error() returns 5 = JSON_ERROR_UTF8 .

    
asked by anonymous 10.11.2015 / 16:18

2 answers

2

To solve the coding problem, I ran the array by creating a new one and using utf8_encode() to leave everything with utf-8 .

$novo = array();
foreach ($result as $key => $value) {
   foreach ($value as $k => $v) {
     $novo[$key][$k] = utf8_encode($v);
   }
}

echo json_encode($novo);
    
10.11.2015 / 20:52
2

Alternatively, you can use mysql's SET names fault:

$dbh = new PDO(...);
$dbh->exec('SET names utf8');

Being Firebird (I could not test):

$str_conn = 'firebird:host=localhost;dbname=/var/lib/firebird/2.5/data/employee.fdb;charset=UTF8';
$dbh = new PDO($str_conn, 'sysdba', 'senha');

Maybe it does not solve, an alternative to your answer would be to use array_map

$result = array_map('utf8_encode', $result);

Or array_walk_recursive for cases where you have multidimensional arrays:

function encode_items(&$item, $key)
{
    $item = utf8_encode($item);
}

array_walk_recursive($jobs, 'encode_items');

Source: link

    
10.11.2015 / 21:06