我有从数据库中获取数据的脚本.这些数据是多维数组.你可以说数组数组和输出以json格式编码.我的问题是如何从每个json值的开头和结尾删除空格.
输出是: -
{"post":{"id":"4","image":" LIVE_3.JPG ","video":"LIVE_3.MOV","category":""}},
{"post":{"id":"3","image":" LIVE_2.JPG ","video":"LIVE_2.MOV","category":""}},
{"post":{"id":"2","image":"LIVE_1.JPG","video":"LIVE_1.MOV","category":""}}
Run Code Online (Sandbox Code Playgroud)
需要和预期: -
{"post":{"id":"4","image":"LIVE_3.JPG ","video":"LIVE_3.MOV","category":""}},
{"post":{"id":"3","image":"LIVE_2.JPG ","video":"LIVE_2.MOV","category":""}},
{"post":{"id":"2","image":"LIVE_1.JPG","video":"LIVE_1.MOV","category":""}}
Run Code Online (Sandbox Code Playgroud)
我的代码是: -
$query = "SELECT * FROM data ORDER BY ID DESC ";
$result = mysql_query($query,$link) or die('Errant query: '.$query);
/* create one master array of the records */
$posts = array();
if(mysql_num_rows($result)) {
while($post = mysql_fetch_assoc($result)) {
$posts[] = array('post'=>$post);
}
}
/* output in necessary format */
header('Content-type: application/json');
echo json_encode(array('posts'=>$posts));
/* disconnect from the db */
@mysql_close($link);
Run Code Online (Sandbox Code Playgroud)