doubianyu7844 2011-06-16 19:09 采纳率: 0%
浏览 28
已采纳

合并数据结构

I have a set of structured data, that I'm trying to merge together, based on a set of conditions.

There is a set of rows, returned from the db. Each row has a course id, and a discipline id. There's an equal amount of disciplines in each course, but some disciplines are repeated in both courses.

I want to build a data structure where if the discipline is in both courses, then it only appears once on a line in a new data structure, and nothing else, but if there are two unmatched disciplines, then they are both included in a new course.

The code I'm using so far, is filtering and removing the keys that are duplicated, and adding them to a new array. This works fine.

However, because I can't control the order in which the data comes (don't ask) I'm having troubles making sure that each line has either a discipline that appears in both courses, or one of each.

I think I need some techniques to help deal with this, and was wondering if anyone had come across this before. I want to avoid making many consecutive loops, if possible.

Thanks.

edit: messy and horrible code below:

function buildDisciplineMap(){
        $sql = "SELECT  [idcurso]
                          ,[idversao]
                          ,[ordem]
                          ,[bloco]
                          ,[obsbloco]
                          ,[iddisciplina] as idd
                          ,cast([iddisciplina] as TEXT) as iddisciplina 
                          ,[descdisciplina]
                          ,[iddepartamento]
                          ,[descdepartamento]
                          ,[ects]
                          ,[horas]
                          ,[idregente]
                          ,[regente]
                          ,[idregente1]
                          ,[regente1]
                          ,[idregente2]
                          ,[regente2]
                          ,[idregente3]
                          ,[regente3]
                          ,cast([objectivos] as TEXT) as objectivos
                          ,cast([programa] as TEXT) as programa
                          ,[descdisciplina_en]
                          ,cast([objectivos_en] as TEXT) as objectivos_en
                          ,cast([programa_en] as TEXT) as programa_en
                      FROM [proffile2].[dbo].[vw_site_CursosDisciplinas_FEG]
                      where idcurso = '3512 GE' or idcurso = '3513 ECON' order by idcurso desc ";
        $discs = $this->returnObject($sql);

        $map = new stdClass();

        // find blocos, and titles

        foreach ($discs as $key => $value) {
            if (isset($map->bloco[$value->bloco])) {
                // block already exists
            } else {
                #echo "making new block";
                $map->bloco[$value->bloco] = new stdClass();


            }

            if (strlen($value->obsbloco)>1) {
                $map->bloco[$value->bloco]->title = $value->obsbloco;
            }

        }

        foreach ($map->bloco as $keybloco => $value) {

            $map->bloco[$keybloco]->lines = array();

            $processed_ids = array();

            foreach ($discs as $kd => $vd) {

                if ($vd->bloco == $keybloco) {

                    // check if this discipline occurs more than once in this block
                    foreach ($discs as $kdd => $vdd) {
                        if ($vdd->iddisciplina == $vd->iddisciplina && $kdd != $kd && !in_array($kd,$processed_ids) && !in_array($kdd,$processed_ids)) {


                            // this discipline is for both courses
                            $details = array();
                            $details['both'] = $vd;


                            $map->bloco[$keybloco]->lines[] = $details;
                            array_push($processed_ids, $kd, $kdd);
                            unset($discs[$kdd]);
                            unset($discs[$kd]);
                            break;
                        }


                    }

                }
            }
        }

        $processed_ids = array();

        foreach ($discs as $key => $value) {
            echo $value->idcurso."
";
        }

        foreach ($discs as $kd => $vd) {

            $bloco = $vd->bloco;
            $lastidx =sizeof($map->bloco[$bloco]->lines)-1;

            $line = $map->bloco[$bloco]->lines[$lastidx];

            echo sizeof($map->bloco[$bloco]->lines);
            #pr($line);
            if (isset($line['both'])) {
                echo "omog - 
 ";
                $map->bloco[$bloco]->lines[][$vd->idcurso] = $vd;
                unset($discs[$kd]);
                continue;
            }
            #pr($line['both']->idcurso);

            foreach ($map->bloco[$bloco]->lines as $k => $v) {

                echo $k."-";

                 #echo $v['3513 ECON']->idcurso;
            }

            if ($line[$vd->idcurso]) {
                echo 'add';
                $map->bloco[$bloco]->lines[][$vd->idcurso] = $vd;
            } else {
                echo 'fill';
                $map->bloco[$bloco]->lines[sizeof($map->bloco[$bloco]->lines)-1][$vd->idcurso] = $vd;
            }






        }
        echo sizeof($discs);

        return $map;


    }
  • 写回答

1条回答 默认 最新

  • doutui9606 2011-06-16 19:15
    关注

    You said "don't ask", but I've got to: why can't you control the order of the rows? Aren't you the one writing the database query?

    If you think fixing the order of the rows will help you parse and build a new data structure better, why not make sorting the rows the first step in your process? Or, is the data set too large?

    You might find some of PHP's array set manipulations to be of use. i.e. array_diff, array_intersect_key etc.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大