PHP无限极分类,如何查出所有分类关联数据后拼接数组

发布于 2022-09-11 20:48:36 字数 5280 浏览 12 评论 0

现在有两个数组,一个是查询分类表的数组list,一个是根据分类表的id去查询相关分类下数据的详情表result,分类表的id等于详情表的aid。
这是分类表,数组list:

array(4) {
  [0] => array(5) {
    ["id"] => string(1) "1"
    ["pid"] => string(1) "0"
    ["name"] => string(21) "一、履历类材料"
    ["orders"] => string(1) "1"
    ["status"] => string(1) "1"
  }
  [1] => array(5) {
    ["id"] => string(1) "4"
    ["pid"] => string(1) "0"
    ["name"] => string(84) "二、学历学位职业资格技术职务(职称)科研学术水平培训材料"
    ["orders"] => string(1) "4"
    ["status"] => string(1) "1"
  }
  [2] => array(5) {
    ["id"] => string(1) "5"
    ["pid"] => string(1) "4"
    ["name"] => string(22) "1、学历学位材料"
    ["orders"] => string(1) "1"
    ["status"] => string(1) "1"
  }
  [3] => array(5) {
    ["id"] => string(1) "6"
    ["pid"] => string(1) "4"
    ["name"] => string(55) "2、职业资格和专业技术职务(职称)材料"
    ["orders"] => string(1) "2"
    ["status"] => string(1) "1"
  }
}

这是详情表,数组result:

array(3) {
  [0] => array(11) {
    ["id"] => string(1) "5"
    ["uid"] => string(1) "1"
    ["aid"] => string(1) "1"
    ["prefix"] => string(1) "1"
    ["name"] => string(12) "这是履历"
    ["src"] => string(44) "/Uploads/archives/1/15605337171160204818.png"
    ["time"] => string(10) "2019-06-15"
    ["page"] => string(1) "1"
    ["sort"] => string(1) "1"
    ["status"] => string(1) "1"
    ["uploadtime"] => NULL
  }
  [1] => array(11) {
    ["id"] => string(2) "12"
    ["uid"] => string(1) "1"
    ["aid"] => string(1) "1"
    ["prefix"] => string(1) "1"
    ["name"] => string(13) "这是履历1"
    ["src"] => string(44) "/Uploads/archives/1/15605762101651474031.xls"
    ["time"] => string(10) "2019-06-15"
    ["page"] => string(1) "1"
    ["sort"] => string(1) "0"
    ["status"] => string(1) "1"
    ["uploadtime"] => NULL
  }
  [2] => array(11) {
    ["id"] => string(2) "15"
    ["uid"] => string(1) "1"
    ["aid"] => string(1) "6"
    ["prefix"] => string(3) "4-2"
    ["name"] => string(12) "职称材料"
    ["src"] => string(43) "/Uploads/archives/1/1560610935753658025.xls"
    ["time"] => string(10) "2019-06-15"
    ["page"] => string(1) "1"
    ["sort"] => string(1) "3"
    ["status"] => string(1) "1"
    ["uploadtime"] => NULL
  }
}

我想要的数组array:

array(4) {
  [0] => array(5) {
    ["id"] => string(1) "1"
    ["pid"] => string(1) "0"
    ["name"] => string(21) "一、履历类材料"
    ["orders"] => string(1) "1"
    ["status"] => string(1) "1"
        array(2) {
          [0] => array(11) {
            ["id"] => string(1) "5"
            ["uid"] => string(1) "1"
            ["aid"] => string(1) "1"
            ["prefix"] => string(1) "1"
            ["name"] => string(12) "这是履历"
            ["src"] => string(44) "/Uploads/archives/1/15605337171160204818.png"
            ["time"] => string(10) "2019-06-15"
            ["page"] => string(1) "1"
            ["sort"] => string(1) "1"
            ["status"] => string(1) "1"
            ["uploadtime"] => NULL
          }
          [1] => array(11) {
            ["id"] => string(2) "12"
            ["uid"] => string(1) "1"
            ["aid"] => string(1) "1"
            ["prefix"] => string(1) "1"
            ["name"] => string(13) "这是履历1"
            ["src"] => string(44) "/Uploads/archives/1/15605762101651474031.xls"
            ["time"] => string(10) "2019-06-15"
            ["page"] => string(1) "1"
            ["sort"] => string(1) "0"
            ["status"] => string(1) "1"
            ["uploadtime"] => NULL
          }
      }
  }
  [1] => array(5) {
    ["id"] => string(1) "4"
    ["pid"] => string(1) "0"
    ["name"] => string(84) "二、学历学位职业资格技术职务(职称)科研学术水平培训材料"
    ["orders"] => string(1) "4"
    ["status"] => string(1) "1"
  }
  [2] => array(5) {
    ["id"] => string(1) "5"
    ["pid"] => string(1) "4"
    ["name"] => string(22) "1、学历学位材料"
    ["orders"] => string(1) "1"
    ["status"] => string(1) "1"
        array(1) {
          [0] => array(11) {
            ["id"] => string(2) "15"
            ["uid"] => string(1) "1"
            ["aid"] => string(1) "6"
            ["prefix"] => string(3) "4-2"
            ["name"] => string(12) "职称材料"
            ["src"] => string(43) "/Uploads/archives/1/1560610935753658025.xls"
            ["time"] => string(10) "2019-06-15"
            ["page"] => string(1) "1"
            ["sort"] => string(1) "3"
            ["status"] => string(1) "1"
            ["uploadtime"] => NULL
          }
  }
  [3] => array(5) {
    ["id"] => string(1) "6"
    ["pid"] => string(1) "4"
    ["name"] => string(55) "2、职业资格和专业技术职务(职称)材料"
    ["orders"] => string(1) "2"
    ["status"] => string(1) "1"
  }
}

两个数组生成的代码为:

$list = M('archives')->where($where)->select();
$result = M('user_archives')->where(array('status'=>1,'uid'=>$uid))->order('sort')->select();

我的思路可能有问题,应该是查到了全部分类,然后在循环分类ID,再用循环的分类ID去查详情表的内容,这一块用递归的方式试了好多次,可是总是得不到想要的结果,弄了一个通宵了没弄好,技术太差了,只能向大家求救了,谢谢各位大神~

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小…楫夜泊 2022-09-18 20:48:36

搞定了,这么写:
mark一下

$uid = I('get.uid');
$where['status'] = 1;
$list = M('archives')->where($where)->select();
$result = M('user_archives')->where(array('status'=>1,'uid'=>$uid))->order('sort')->select();
foreach ($list as $value) {
    $data[] = M('user_archives')->where(array('status'=>1,'uid'=>$uid,'aid'=>$value['id']))->order('sort')->select();
}
foreach ($list as $key => $value) {
    $list[$key]['child'] = $data[$key];
}
一个人的夜不怕黑 2022-09-18 20:48:36

我就没有查那么多次sql,我直接查出全部,分出一级后再递归

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文