我有两个表,第一个是tbl_blogs,其中包含所有博客类别,例如,男人,女人等。
第二个表包含博客的相关类别信息。
$query1=mysqli_query($link,"select * from tbl_blog_cat");
$array1=array();
$array2=array();
$rows=array();
$row=array();
if(mysqli_affected_rows($link)){
while($row=mysqli_fetch_array($query1)){
$catId=$row['id'];
$catName=$row['title'];
$q2=mysqli_query($link,"select * from tbl_blogs where
blog_cat=$catId");
if(mysqli_affected_rows($link)){
while($row2=mysqli_fetch_array($q2)){
$rows['BlogId']=$row2['BlogId'];
$rows['Title']=$row2['Title'];
$rows['blog_cat']=$row2['blog_cat'];
$rows['Description']=$row2['Description'];
$rows['CoverImage']=$row2['CoverImage'];
array_push($array1,$rows);
}
$array2[$catName]=$array1;
}
}
echo json_encode($array2);
}
-----------------这是我的输出-------------------
第二组女性类别具有与上一类别相同的ID。 我希望将其删除,否则它会让我知道我的代码中是否有任何错误。
谢谢。enter code here
{ "Men": [ { "BlogId": "25", "Title": "layer", "blog_cat": "5", "Description": "a sheet, quantity, or thickness of material, typically one of several, covering a surface or body", "CoverImage": "Screenshot_from_2018-12-28_23-12-51.png" } ], "Women": [ { "BlogId": "25", "Title": "layer", "blog_cat": "5", "Description": "a sheet, quantity, or thickness of material, typically one of several, covering a surface or body", "CoverImage": "Screenshot_from_2018-12-28_23-12-51.png" }, { "BlogId": "19", "Title": "Blog Test Man", "blog_cat": "6", "Description": "no dsc", "CoverImage": "Screenshot_from_2019-01-07_16-54-44.png" }, { "BlogId": "22", "Title": "layer", "blog_cat": "6", "Description": "a sheet, quantity, or thickness of material, typically one of several, covering a surface or body", "CoverImage": "Screenshot_from_2018-12-28_23-12-51.png" }, { "BlogId": "24", "Title": "Blog Test Man", "blog_cat": "6", "Description": "no dsc", "CoverImage": "Screenshot_from_2019-01-07_16-54-44.png" } ] }
答案 0 :(得分:1)
您一直在填充$array1
,但是在开始新类别时并没有清除它。
尝试将$array1=array();
放在第一个while
之后