本文章仅为记录笔记
代码语言:javascript复制<?php
header("Content-type:text/html;charset=utf-8");//字符编码设置
if (!$_GET) {
die("缺少参数");
}
$json = '';
$data = array();
class User
{
public $cate_id;
public $cate_name;
public $ishaveChild;
public $children;
}
include '../conn.php';
$sql="SELECT * FROM shop_class";
$result=$conn->query($sql);
if($result){
//echo "查询成功";
while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC))
{
$user = new User();
$user->cate_id = $row["id"];
// $user->ids = $row["ids"];
$user->cate_name = $row["class_name"];
$user->ishaveChild = 'true';
// $user->sp_sb = $row["sp_sb"];
$md=$row["class_code"];
//查询商品
$cx_shop="SELECT * FROM `shop` where shop_class='$md'";
$re=$conn->query($cx_shop);
$user->children = array();
while ($reo=mysqli_fetch_array($re,MYSQLI_ASSOC)){
$count=count($reo);//不能在循环语句中,由于每次删除 row数组长度都减小
for($i=0;$i<$count;$i ){
unset($reo[$i]);//删除冗余数据
}
array_push($user->children,$reo);
}
$str=json_encode($children);//将数组进行json编码
$data[]=$user;
}
$json = json_encode($data);//把数据转换为JSON数据.
echo $json;
}else{
echo "查询失败";
}
截图:
Json化后可见多层嵌套:
可用于小程序以及其他业务场景接口开发