php的循环最后多出一个逗号
<?phpheader("content-type:text/html;charset=utf-8");$pdo=newPDO("mysql:host=localhost...
<?phpheader("content-type:text/html;charset=utf-8"); $pdo = new PDO("mysql:host=localhost;dbname=blog;charset=utf8;","root","root"); $query="SELECT * FROM `article` "; ?> {"code":0,"msg":"","count":1000,"data":[<?php foreach ($pdo->query($query) as $row) { ?>{"id":<?php echo $row[id]; ?>,"title":"user-0","content":"女","category":"城市-0"},<?php } ?>]}
展开
2个回答
展开全部
你这样转换JSON不考虑后期维护吗?
$pdo = new PDO("mysql:host=localhost;dbname=blog;charset=utf8;", "root", "root");
$statement = $pdo->query("SELECT * FROM `article` ");
$data = [
'code' => 0,
'count' => 1000,//$statement->columnCount()
'data' => array_map(function ($item) {
return [
'id' => $item['addr_id'],
'title' => 'user-0',
'content' => '女',
'category' => '城市-0'
];
}, $statement->fetchAll())
];
echo json_encode($data, JSON_UNESCAPED_UNICODE);
追问
本来想不要数组直接写的,看来确认缺乏考虑。要向您多学习。
2017-09-29
展开全部
下面仅供参考:
$array = [1,2,3,4,];
// 数组转化成字符串 结果为 $qq = "1,2,3,4";
$string = implode(',', $array);
// 字符串转化为数组, 结果为 $array1 = [1,2,3,4];
$array1 = explode(',', $string);
$array = [1,2,3,4,];
// 数组转化成字符串 结果为 $qq = "1,2,3,4";
$string = implode(',', $array);
// 字符串转化为数组, 结果为 $array1 = [1,2,3,4];
$array1 = explode(',', $string);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询