In the "while" method adds to the array

0

I'm trying to produce a script that when I query in mysql it returns an array, so far ok, but the goal is to have this query turn an edited json. Example:

{"changelog":{"count":2,"news":{"ola":"testando1","ola2":"testando2"}}}

And in C # I make a code that returns this and put it in a TextBox. But I do not know how to add an item to an array when it returns the data in array.

$array1 = array();
while( $row = mysql_fetch_array($query)){
   // adiciona no array a $row[1] e $row[2] por exemplo: no lugar do 'ola' fica o $row[1] e no lugar do 'testando1' fica o $row[2]
}

however the query we will assume is 30 items the query is limited to LIMIT 0,10

TABLE:

CAMPO        | VALOR
ID           | TIPO INT, NOT NULL, AUTO INCREMENT
TITLE        | TIPO TEXT, NOT NULL // $row[1]
DESCRIPTION  | TIPO LONGTEXT, NOT NULL // $row[2]
     

%pr_e%

    
asked by anonymous 26.04.2016 / 17:52

2 answers

2

Dude, see if that's more or less what you want:

<?php
$aux = array();
while($dado = mysqli_fetch_array($sql)){
    $title = $dado['title'];
    $valor = $dado['valor'];

    $aux2 = array("titulo" => $title, "valor" => $valor);

    array_push($aux, $aux2);

}
$array = array("cahangelog" => array("count" => 2, "news" => $aux));

echo json_encode($array);

OUTPUT:

{"cahangelog":{"count":2,"news":[{"titulo":"testando","valor":"valor teste"}]}} 
    
26.04.2016 / 21:41
-1

See if this resolves:

$array1 = array();
while( $row = mysql_fetch_array($query)){
   $array1[]  = array("valor 1", "valor 2");
}
    
27.04.2016 / 00:52