开发者

Merging data structures

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]
                      whe开发者_JS百科re 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."\n";
        }

        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 - \n ";
                $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;


    }


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜