Return 2 PDO queries in JSON

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 one 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"
    }
  }
}

How would I return both in JSON ? I tried to join the two array and then it gives json_encode() , but it did not work.

~ edit : After doing a foreach traversing the data with utf8_encode() , cosegui ... but there 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);
    
asked by anonymous 30.07.2016 / 02:09

1 answer

0

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

On error when using json_encode() function is due to array not being properly encoded, ie UTF-8 .

To solve this you can do:

  • Use the utf8_encode() function on each item of array .
  • Pass the constant JSON_UNESCAPED_UNICODE to the json_encode function as a parameter.
  • 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);
    

    View demonstração

        
    30.07.2016 / 03:57