0

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
0

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.