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 functionarray_merge
and then use thejson_encode
.– stderr
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 =)
– Noah
The way I showed it worked there too?
– stderr
It did work @zekk Thank you so much ! =)
– Noah