PHP JSON output with mysql for tag-it
Вопрос
I'm trying to get the tag-it jquery plugin to work with a json string. Currenttly i'm getting my values from the database like this:
$query = sprintf(
'SELECT
t.tag
FROM
tags AS t
');
$row_set= array();
if($result = mysqli_query($db, $query))
{
// fetch data
while ($row = mysqli_fetch_assoc($result))
{
$row_set[] = $row;
}
// set the output
echo json_encode($row_set);
}
which gives the following output when called in AJAX:
[{"tag":"test"},{"tag":"tests"}]
But I have to output a JSON string in the following format:
["android-intent","animate","architecture","artificial-intelligence","attributes"]
How can i achieve this?
Решение
When you use row_set[] = $row;
most likely $row
looks like this $row['tag'] = 'test
that is why you are having JSON object format;
Try
$row_set[] = $row['tag'];
Make sure you also set the seound parameter to true
to make JSON
always return array
json_encode($row_set,true);
This would return the it as array not as a object
Другие советы
That should fix it, by adding string into you arrow instead of adding arrays into you array.
$row_set[] = $row[0]; or $row_set[] = $row['tag'];
Не связан с StackOverflow