我想要 PHP 中的 JSON 格式,但我无法做到这一点。
{
"couresList_PVP": [
{
"pvp_ad_chptr_id": "9",
"pvp_ad_chptr_un_id": "1526249608",
"pvp_ad_chptr_name": "54654",
"offer_chapter_PVP": [
{
"pvp_ad_chptr_offr_id": "4",
"pvp_ad_chptr_un_id": "1526249608",
"pvp_ad_chptr_offr_un_id": "Offer-1526249608"
},
{
"pvp_ad_chptr_offr_id": "3",
"pvp_ad_chptr_un_id": "1526249608",
"pvp_ad_chptr_offr_un_id": "Offer-1526249608"
}]
},]
}
我的 PHP 代码在这里。我将如何进行此分组?我收到 JSON 响应,但采用不同的数组结构而不是嵌套结构。
$sql = mysqli_query($this->db,"SELECT * from `pvp_admin_chptr_list` order by pvp_ad_chptr_id desc");
if(mysqli_num_rows($sql) > 0){
while($res=mysqli_fetch_assoc($sql)){
$url=$this->site_url.'images_console/chapter_banner_console/'.$res['pvp_ad_chptr_banner'];
$video=$this->site_url.'video_console/'.$res['pvp_ad_chptr_video_name'];
$images=array('pvp_ad_chptr_banner'=>$url,'pvp_ad_chptr_video_name'=>$video);
$data['couresList_PVP'][]=array_merge($res,$images);
$ofr_sql = mysqli_query($this->db,"SELECT * from `pvp_admin_chptr_offers` where pvp_ad_chptr_un_id='$res[pvp_ad_chptr_un_id]' order by pvp_ad_chptr_offr_id desc");
if(mysqli_num_rows($ofr_sql) > 0){
while($ofr_res=mysqli_fetch_assoc($ofr_sql)){
$data['offer_chapter_PVP'][]=$ofr_res;
}
}else{
$data['offer_chapter_PVP'][]=array("status"=>"No Data FOund");
}
}
//$data2=array_merge($data['couresList_PVP'],$data['offer_chapter_PVP']);
//$data[]=array_push( $data['couresList_PVP'],$data['offer_chapter_PVP']);
//$data=
// If success everything is good send header as "OK" and user details
$this->response($this->json($data), 200);
}
您可以使用数组组合,然后将其编码为 json。请检查下面的代码。
$response = array();
$responseObj = array();
$courseList = array();
$courseArr = array();
while($res=mysqli_fetch_assoc($sql)){
$courseList['pvp_ad_chptr_id'] = $res['id'];
$courseList['pvp_ad_chptr_un_id'] = $res['pvp_ad_chptr_un_id'];
$courseList['pvp_ad_chptr_name'] = $res['pvp_ad_chptr_name'];
$ofr_sql = mysqli_query($this->db,"SELECT * from `pvp_admin_chptr_offers` where pvp_ad_chptr_un_id='$res[pvp_ad_chptr_un_id]' order by pvp_ad_chptr_offr_id desc");
if(mysqli_num_rows($ofr_sql) > 0){
while($ofr_res=mysqli_fetch_assoc($ofr_sql)){
$courseList['offer_chapter_PVP'][]=$ofr_res;
}
}else{
$courseList[$i]['offer_chapter_PVP'][]=array("status"=>"No Data FOund");
}
$courseArr[] = $courseList;
}
$response['couresList_PVP'] = $courseArr;
$responseObj = $response;
echo '<pre>';
echo json_encode($responseObj);
echo '</pre>';
避免重复访问数据库非常重要。相反,请使用 JOIN 查询来获取一次旅程中所需的所有内容。
迭代时,为每个唯一的
pvp_ad_chptr_un_id
填充父数据集。将新元素推入结果数组中作为基线数据的参考。然后,当在另一行中找到相同的 id 时,将子数组推入该组。
如果
list
行可能在 offers
表中没有任何子级,则在将子级数据集推入组之前使用 LEFT JOIN
检查非空值。
未经测试的代码:
$sql = <<<SQL
SELECT lst.pvp_ad_chptr_id,
lst.pvp_ad_chptr_un_id,
lst.pvp_ad_chptr_name,
lst.pvp_ad_chptr_banner,
lst.pvp_ad_chptr_video_name,
ofr.pvp_ad_chptr_offr_id,
ofr.pvp_ad_chptr_offr_un_id
FROM pvp_admin_chptr_list lst
JOIN pvp_admin_chptr_offers ofr ON lst.pvp_ad_chptr_un_id = ofr.pvp_ad_chptr_un_id
ORDER BY lst.pvp_ad_chptr_id DESC,
ofr.pvp_ad_chptr_offr_id DESC
SQL;
$data = [];
foreach ($this->db->query($sql) as $row) {
if (!isset($ref[$row['pvp_ad_chptr_un_id']])) {
$ref[$row['pvp_ad_chptr_un_id']] = [
'pvp_ad_chptr_id' => $row['pvp_ad_chptr_id'],
'pvp_ad_chptr_un_id' => $row['pvp_ad_chptr_un_id'],
'pvp_ad_chptr_name' => $row['pvp_ad_chptr_name'],
'pvp_ad_chptr_banner' => $this->site_url . 'images_console/chapter_banner_console/' . $row['pvp_ad_chptr_banner'],
'pvp_ad_chptr_video_name' => $this->site_url . 'video_console/' . $row['pvp_ad_chptr_video_name'],
'offer_chapter_PVP' => [],
];
$data['courseList_PVP'][] =& $ref[$row['pvp_ad_chptr_un_id']];
}
$ref[$row['pvp_ad_chptr_un_id']]['offer_chapter_PVP'][] = [
'pvp_ad_chptr_offr_id' => $row['pvp_ad_chptr_offr_id'],
'pvp_ad_chptr_un_id' => $row['pvp_ad_chptr_un_id'],
'pvp_ad_chptr_offr_un_id' => $row['pvp_ad_chptr_offr_un_id'],
];
}
$this->response($this->json($data), 200);