I am creating a json array with data from mysql:
$array = $db->query("SELECT .............")->fetchAll(PDO::FETCH_ASSOC);
print json_encode($array);
This returns:
[{"animal":"alpaca"},{"animal":"buffalo"},{"animal":"cat"},{"animal":"tiger"}]
But I need it to return data with the following syntax:
["alpaca","buffalo","cat","tiger"]
Any hints on how to achieve this?
使用array_values()
解决您的问题:
$array = json_encode(array_values($array));
As you can simply use array_values
over here but for an alteration you can also use array_column
like as
$array = $db->query("SELECT .............")->fetchAll(PDO::FETCH_ASSOC);
print json_encode(array_column($array,'animal'));
Note:
array_column()
requires PHP > 5.5.0
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.