Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6024911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:13:32+00:00 2026-05-23T04:13:32+00:00

I have a set of structured data, that I’m trying to merge together, based

  • 0

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."\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;


    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T04:13:33+00:00Added an answer on May 23, 2026 at 4:13 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a set of data that is structured like this: ItemA.GroupA ItemB.GroupA ItemC.GroupB
I have a small set of structured data items that I would like embedded
I have a set of core, complicated JavaScript data structures/classes that I'd like to
I have some nicely-structured data that looks like this: CREATE TABLE SourceBodyPartColors ( person_ID
I've got a very large xml data set that is structured like the following:
I have a website that needs to pull information from two diffferent XML data
I have set up a circular linked list data structure that represents a word,
I have a set of data in one JSON structure: [[task1, 10, 99], [task2,
I have several complex data structures like Map< A, Set< B > > Set<
I have a vector<set<char> > data structure (transactions database) and I want to know

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.