Reputation: 1834
i retrieve and print data from MySQL database in json
method:
$ret = array();
$fileName = Access::FetchDB("SELECT name FROM tags");
$ret[]= $fileName;
echo json_encode($ret);
Now output is:
[[{"name":"test1"},{"name":"test2"},{"name":"test3"}]]
But i need to This output:
["test1","test2","test3"]
How do i print this?
Upvotes: 1
Views: 98
Reputation: 19016
[[{"name":"test1"},{"name":"test2"},{"name":"test3"}]]
is like having this
array(array(array('name' => 'test1'), array('name' => 'test2'), array('name' => 'test3')));
First, do not do this:
$ret[]= $fileName;
And keep only $fileName
which should be something like this:
array(array('name' => 'test1'), array('name' => 'test2'), array('name' => 'test3'));
The better would even be to have array('test1', 'test2', 'test3')
and encode it without array_values(). You could do it in php side:
$ret = array();
$fileName = Access::FetchDB("SELECT name FROM tags");
foreach($fileName as $key => $value)
$ret[] = $value['name'];
echo json_encode($ret);
Upvotes: 5
Reputation: 723
Try this solution:
$ret = array();
$fileNames = Access::FetchDB("SELECT name FROM tags");
$ret = array_values($fileNames);
echo json_encode($ret);
Upvotes: 1