PHP json_encode return rows as arrays instead of objects
From my experience, when I json_encode data from a mysql table, the output is an object containing each row as an object within. What I want to do is to get each row as an array, like the example below (don't need the column names in there). How can I do that?
{ "Data": [
["1","Internet Explorer 4.0","Win 95+","4","X","1"],
["2","Internet Explorer 5.0","Win 95+","5","C","2"],
开发者_运维技巧["3","Internet Explorer 5.5","Win 95+","5.5","A","3"],
["4","Internet Explorer 6","Win 98+","6","A","4"],
] }
Use mysql_fetch_row
[docs] to get each row. This will get a row as a numerical array and those are also encoded as JSON arrays:
$data = array();
while(($row = mysql_fetch_row($result))) {
$data[] = $row;
}
echo json_encode(array('Data' => $data));
NOT CHECKED in interpreter: You can change the type of variable by simple types convertion:
$data = DB::get('SELECT * FROM `table` WHERE 1');
echo json_encode(array('Data' => (array)$data));
Also you can use next function: array mysql_fetch_array ( resource $result [, int $result_type ] )
精彩评论