thinkphp5数据的无限分类

1.方式一

<?php
	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": ""
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

原克技术

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值