请告诉我如何以下面的格式从MySql数据库中获取数据。
现在,我正在以下面的格式获取数据
$sql="SELECT c.ad_id FROM `category_rel` c INNER JOIN tbl_category_master d ON c.category_id= d.fld_category_id WHERE d.fld_category_name IN ($category)";
$result1=$con->query($sql);
while ($row = mysqli_fetch_assoc($result1)) {
$output[] = $row;
}
$ids=json_encode(array($output));
结果:
[[{"ad_id":"2"},{"ad_id":"11"},{"ad_id":"28"},{"ad_id":"62"},{"ad_id":"64"}]]
但是,我想要以下格式:
["2","11","28","62"]
它不应该带有列名。
答案 0 :(得分:1)
尝试使用$output[] = $row['ad_id'];
代替$output[] = $row;
[编辑] 同样删除array()函数:
$sql="SELECT c.ad_id FROM `category_rel` c INNER JOIN tbl_category_master d ON c.category_id= d.fld_category_id WHERE d.fld_category_name IN ($category)";
$result1=$con->query($sql);
while ($row = mysqli_fetch_assoc($result1)) {
$output[] = $row['ad_id'];
}
$ids=json_encode($output);
问候。
答案 1 :(得分:0)
如果您想从while循环中执行此操作,请使用它:
$sql="SELECT c.ad_id FROM `category_rel` c INNER JOIN tbl_category_master d ON c.category_id= d.fld_category_id WHERE d.fld_category_name IN ($category)";
while ($row = mysqli_fetch_assoc($result1)) {
$output[] = $row['ad_id']; //may be this will help Not sure the structure
}
$ids = json_encode(array($output));
如果您需要输出数组,那么只需使用:
$json = '[[{"ad_id":"2"},{"ad_id":"11"},{"ad_id":"28"},{"ad_id":"62"},{"ad_id":"64"}]]';
$arr = json_decode($json, true);
$arr = array_column($arr[0], 'ad_id');
echo '<pre>';
print_r($arr);
结果:
Array
(
[0] => 2
[1] => 11
[2] => 28
[3] => 62
[4] => 64
)