I have this piece of code:
Tipo_Id = mysql_real_escape_string($_REQUEST["tipo"]);
$Sql = "SELECT DISTINCT(tabveiculos.Marca_Id), tabmarcas.Marca_Nome
FROM tabmarcas, tabveiculos
WHERE tabmarcas.Tipo_Id = '$Tipo_Id'
AND tabmarcas.Marca_Id = tabveiculos.Marca_Id
ORDER BY tabmarcas.Marca_Nome Asc";
$Query = mysql_query($Sql,$Conn) or die(mysql_error($Conn));
$marcas = array();
while ($Rs = mysql_fetch_array($Query)) {
$marcas[] = array(
$Rs['Marca_Id'] =>
$Rs['Marca_Nome']
);
}
echo ( json_encode($marcas) );
this returns a result like this:
[{"2":"Chevrolet"},{"7":"Citro"},{"4":"Fiat"},{"3":"Ford"},{"6":"Peugeot"},{"1":"Volkswagen"}]
so how i can change to returns like this:
{"2":"Chevrolet","7":"Citro","4":"Fiat","3":"Ford","6":"Peugeot","1":"Volkswagen"}
laurent
91.2k83 gold badges310 silver badges445 bronze badges
asked Apr 22, 2012 at 16:22
Alvaro Louzada
4331 gold badge6 silver badges23 bronze badges
1 Answer 1
You're currently creating a new array for each key value pair, hence, ending up with a multidimensional array. Do the following instead.
while ($Rs = mysql_fetch_array($Query)) {
$marcas[ $Rs['Marca_Id'] ] = $Rs['Marca_Nome'];
}
answered Apr 22, 2012 at 16:24
aziz punjani
25.8k9 gold badges49 silver badges56 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-php