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

Comments

Your Answer

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.