1.方式一
namespace app\index\model;
use think\Model;
class Classfiy extends Model{
protected $table = 'classfiy';
// 方式一
public function htmllist(){
$data=$this->select();
return $this->htmllistM($data);
}
public function htmllistM($data,$pid=0,$lev=''){
static $arr=array();
foreach($data as $key=>$value){
if($value['pid']==$pid){
$value['lev']=$lev;
$arr[]=$value;
$this->htmllistM($data,$value['id'],$lev.'!--');
}
}
return $arr;
}
// 方式二
public function getHtmlList(&$result = [], $pid = 0, $prefx = '')
{
$data = $this->select();
// $data = $this->select()->toarray();//有时出错
foreach ($data as $value) {
if ($value['pid'] == $pid) {
$value['name'] = $prefx.$value['name'];
$result[] = $value;
$this->getHtmlList($result,$value['id'],$prefx.'|--');
}
}
return $result;
}
}
?>
方式一返回的数据形式是
{
"ret": 200,
"data": [
{
"id": 1,
"name": "手机",
"pid": 0,
"static": 1,
"time": 1588775166,
"last_time": null,
"delete_time": null,
"lev": ""
},
{
"id": 4,
"name": "华为手机",
"pid": 1,
"static": 1,
"time": 1588776159,
"last_time": null,
"delete_time": null,
"lev": "!--"
},
{
"id": 6,
"name": "苹果手机",
"pid": 1,
"static": 1,
"time": 1588776200,
"last_time": null,
"delete_time": null,
"lev": "!--"
},
{
"id": 2,
"name": "电脑",
"pid": 0,
"static": 0,
"time": 1588775190,
"last_time": null,
"delete_time": null,
"lev": ""
},
{
"id": 5,
"name": "华为电脑",
"pid": 2,
"static": 0,
"time": 1588776185,
"last_time": 1588816674,
"delete_time": null,
"lev": "!--"
},
{
"id": 7,
"name": "苹果电脑",
"pid": 2,
"static": 1,
"time": 1588776215,
"last_time": 1588816980,
"delete_time": null,
"lev": "!--"
}
],
"msg": ""}
方式二返回的数据 格式是
{
"ret": 200,
"data": [
{
"id": 1,
"name": "手机",
"pid": 0,
"static": 1,
"time": 1588775166,
"last_time": null,
"delete_time": null
},
{
"id": 4,
"name": "|--华为手机",
"pid": 1,
"static": 1,
"time": 1588776159,
"last_time": null,
"delete_time": null
},
{
"id": 6,
"name": "|--苹果手机",
"pid": 1,
"static": 1,
"time": 1588776200,
"last_time": null,
"delete_time": null
},
{
"id": 2,
"name": "电脑",
"pid": 0,
"static": 0,
"time": 1588775190,
"last_time": null,
"delete_time": null
},
{
"id": 5,
"name": "|--华为电脑",
"pid": 2,
"static": 0,
"time": 1588776185,
"last_time": 1588816674,
"delete_time": null
},
{
"id": 7,
"name": "|--苹果电脑",
"pid": 2,
"static": 1,
"time": 1588776215,
"last_time": 1588816980,
"delete_time": null
}
],
"msg": ""
}
以上是thinkphp5数据的无限分类的详细内容。更多信息请关注PHP中文网其他相关文章!