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去查详情表的内容,这一块用递归的方式试了好多次,可是总是得不到想要的结果,弄了一个通宵了没弄好,技术太差了,只能向大家求救了,谢谢各位大神~

lyf091 2
2019-06-17 提问
2 个回答
0

已采纳

搞定了,这么写:
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];
}
0

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

撰写答案

推广链接