Return 2 PDO queries in JSON

Asked

Viewed 233 times

1

I need to return two queries.

$id = $_POST['id'];
$id = end(explode('editarnovoservico', $id));

$searchid = $pdo->query('SELECT * FROM cad_servicos WHERE id = '.$id);
$searchid = $searchid->fetchAll(PDO::FETCH_ASSOC);
$searchtiposervico = $pdo->query('SELECT * FROM tipo_servicos');
$searchtiposervico = $searchtiposervico->fetchAll(PDO::FETCH_ASSOC);

$array['searchid'] = $searchid;
$array['searchtiposervico'] = $searchtiposervico;

foreach ($array['searchtiposervico'] as $key => $value) {
    $array['searchtiposervico'][$key]['id'] = $value['id'];
    $array['searchtiposervico'][$key]['tipo_servico'] = utf8_encode($value['tipo_servico']);
}

$array['searchid'] = $searchid;
var_dump($array);
$array2['searchtiposervico'] = $searchtiposervico;
var_dump($array2);

The first returns:

array(1) {
  ["searchid"]=>
  array(1) {
    [0]=>
    array(5) {
      ["id"]=>
      string(3) "402"
      ["nome"]=>
      string(7) "bla bla"
      ["cobrado"]=>
      string(1) "1"
      ["id_tipo_servico"]=>
      string(1) "1"
      ["novo"]=>
      string(1) "1"
    }
  }
}

And the second:

array(2) {
  ["searchtiposervico"]=>
  array(8) {
    [0]=>
    array(2) {
      ["id"]=>
      string(1) "1"
      ["tipo_servico"]=>
      string(12) "Desconhecido"
    }
    [1]=>
    array(2) {
      ["id"]=>
      string(1) "2"
      ["tipo_servico"]=>
      string(8) "Liga��es"
    }
    [2]=>
    array(2) {
      ["id"]=>
      string(1) "3"
      ["tipo_servico"]=>
      string(3) "Sms"
    }
    [3]=>
    array(2) {
      ["id"]=>
      string(1) "4"
      ["tipo_servico"]=>
      string(9) "Descontos"
    }
    [4]=>
    array(2) {
      ["id"]=>
      string(1) "5"
      ["tipo_servico"]=>
      string(6) "Multas"
    }
    [5]=>
    array(2) {
      ["id"]=>
      string(1) "6"
      ["tipo_servico"]=>
      string(8) "Parcelas"
    }
    [6]=>
    array(2) {
      ["id"]=>
      string(1) "7"
      ["tipo_servico"]=>
      string(8) "Internet"
    }
    [7]=>
    array(2) {
      ["id"]=>
      string(1) "8"
      ["tipo_servico"]=>
      string(11) "Assinaturas"
    }
  }
}

As I would return the two in one JSON? I tried to put the two together array and then gives a json_encode(), but it didn’t work out.

~Edit: After making a foreach running through the data with utf8_encode(), cosegui... but it is gabiarra. hhaha =)

$array['searchid'] = $searchid;
$array['searchtiposervico'] = $searchtiposervico;   
foreach ($array['searchtiposervico'] as $key => $value) {
    $array['searchtiposervico'][$key]['id'] = $value['id'];
    $array['searchtiposervico'][$key]['tipo_servico'] =  utf8_encode($value['tipo_servico']);
}
echo json_encode($array, true);
  • You can merge the arrays with the function array_merge and then use the json_encode.

  • I tried that too, it didn’t work, I just realized it had something to do with UTF8. I made a foreach going through the data and putting utf8_encode(), it worked... but it is more common still in my code =)

  • The way I showed it worked there too?

  • 1

    It did work @zekk Thank you so much ! =)

1 answer

0


To merge the arrays you can use the function array_merge().

About the error when using the function json_encode() is due to array is not properly coded, that is to say, UTF-8.

To solve this you can do:

  1. Use the function utf8_encode() on each item of array.
  2. Pass constant as parameter JSON_UNESCAPED_UNICODE in functionjson_encode.

Example:

$arraySearchId = array('searchid' => array(
                '0' => array(
                'id' => '1', 
                'nome' => 'bla',
                'cobrado' => '1',
                ),
));

$arraySearchServico = array('searchtiposervico' => array(
                '0' => array(
                'id' => '1', 
                'tipo_servico' => "Ligacões",
                ),

                '1' => array(
                'id' => '2', 
                'tipo_servico' => 'SMS',
                ),              
));

$arrayResultado = array_merge($arraySearchId, $arraySearchServico);

$json = json_encode($arrayResultado, JSON_UNESCAPED_UNICODE);
print_r($json);

See demonstração

Browser other questions tagged

You are not signed in. Login or sign up in order to post.