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 7620191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:52:48+00:00 2026-05-31T03:52:48+00:00

I have a set of data that is not normalized. I do the normalization

  • 0

I have a set of data that is not normalized. I do the normalization in PHP it it works just fine.

The dataset looks like this (screenshot bellow), it is a lot larger tho. I am only interested in orginizing the category and the type. With those two orginized I can make good tables and menu’s.

Problem

Now the problem is, I am switching to an AJAX system, and the data no longer comes into PHP. Instead it comes directly into the page using node.js/mongodb. Is there a way I can do something like this:

<script type="text/javascript">

// Array containing all the objects from the server
// looks like this
var data = [Object, Object, Object];
var artikelen = [];

for(var i=0; i<data.length; i++){
  artikelen[data[i].categorie][data[i].type][] = data[i];
}

</script>

// —————-
OLD SITUATION
//—————–

In PHP I did this:

$sql = "SELECT *
        FROM mydb
        WHERE merk = 'webecos'
        ORDER BY categorie, type, code";

$result = $wpdb->get_results( $sql );

foreach($result as $row){
    $artikelen[$row->categorie][$row->type][] = $row;
}

Now I can make good sorted tables / menu with nested loops. The code I used is this.

<ul id="inkoop_menu">
    <?php foreach ( $artikelen as $categorie => $row ): ?>
    <li>
        <a class="inkoop_button" data-menu="categorie" href="#">
            <?=$categorie; ?>
        </a>
        <ul>
            <?php foreach ( $row as $type => $artikel ): ?>
            <li>
                <a class="inkoop_button" data-menu="type" href="#">
                    <?=$type; ?>
                </a>
            </li>
            <?php endforeach; ?>
        </ul>
    </li>
    <?php endforeach; ?>
</ul>

Edit

Maybe this is a but better to understand. The array I am after looks like this:

array['categorie-name']['type-name'][x] = whole object;

enter image description here

  • 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-31T03:52:49+00:00Added an answer on May 31, 2026 at 3:52 am

    I’m not really sure I totally follow you, but do you mean something like this?

    var data = [Object, Object, Object];
    var artikelen = [];
    
    for(var i=0; i<data.length; i++){
    
        if( !artikelen[data[i].category])
            artikelen[data[i].category] = new Array();
    
        if( !artikelen[data[i].category][data[i].type] )
            artikelen[data[i].category][data[i].type] = new Array();
    
        artikelen[data[i].category][data[i].type].push(data[i]);
    }
    

    More complete example: (also note I put category instead of categorie, change if needed).

    <script language="javascript" type="text/javascript">
    
    var obj1 = new Object();
    obj1.category = 1;
    obj1.type = 2;
    obj1.id = 1;
    
    var obj2 = new Object();
    obj2.category = 1;
    obj2.type = 2;
    obj2.id = 2;
    
    var obj3 = new Object();
    obj3.category = 2;
    obj3.type = 4;
    obj3.id = 3;
    
    
    // Array containing all the objects from the server
    // looks like this
    var data = [obj1, obj2, obj3];
    var artikelen = [];
    
    for(var i=0; i<data.length; i++){
    
        if( !artikelen[data[i].category])
            artikelen[data[i].category] = new Array();
    
        if( !artikelen[data[i].category][data[i].type] )
            artikelen[data[i].category][data[i].type] = new Array();
    
        artikelen[data[i].category][data[i].type].push(data[i]);
    }
    
    alert( artikelen[1][2] ); // expected [object], [object]
    
    alert( artikelen[1][2][0].type ); // expected 2
    
    </script>
    

    Key things to take away from this approach:

    • Check if array at key exists
    • If not, create it
    • .push can be used on a javascript array to add an item to an array

    Using a string as a type for an object:

    var obj1 = new Object();
    obj1.category = 1;
    obj1.type = "hello hello";
    obj1.id = 1;
    
    var obj2 = new Object();
    obj2.category = 1;
    obj2.type = 2;
    obj2.id = 2;
    
    var obj3 = new Object();
    obj3.category = 2;
    obj3.type = 4;
    obj3.id = 3;
    
    
    // Array containing all the objects from the server
    // looks like this
    var data = [obj1, obj2, obj3];
    var artikelen = [];
    
    for(var i=0; i<data.length; i++){
    
        if( !artikelen[data[i].category])
            artikelen[data[i].category] = new Array();
    
        if( !artikelen[data[i].category][data[i].type] )
            artikelen[data[i].category][data[i].type] = new Array();
    
        artikelen[data[i].category][data[i].type].push(data[i]);
    }
    
    alert( artikelen[1][2] ); // expected [object], [object]
    
    alert( artikelen[1]["hello hello"][0].type ); // expected "hello hello"
    

    EDIT

    I gave it some more, thought and after reading this, it turns out that arrays in Javascript are not well suited to be used as associative arrays (like in PHP). In actuality, you are just adding attributes to an object. So making it an object instead is better. For example, the following:

    var obj1 = new Object();
    obj1.category = 1;
    obj1.type = "hello hello";
    obj1.id = 1;
    
    var obj2 = new Object();
    obj2.category = 1;
    obj2.type = 2;
    obj2.id = 2;
    
    var obj3 = new Object();
    obj3.category = 2;
    obj3.type = 4;
    obj3.id = 3;
    
    
    // Array containing all the objects from the server
    // looks like this
    var data = [obj1, obj2, obj3];
    var artikelen = [];
    
    for(var i=0; i<data.length; i++){
    
        if( !artikelen[data[i].category])
            artikelen[data[i].category] = new Object();
    
        if( !artikelen[data[i].category][data[i].type] )
            artikelen[data[i].category][data[i].type] = new Object();
    
        artikelen[data[i].category][data[i].type] = (data[i]);
    }
    
    console.dir( artikelen ); // if using a debugger with console, gives detailed info
    

    Also, if using a debugger, such as Firebug in Firefox, you can see detailed info with the console.dir function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data set that looks like this: 000 100 200 300 010
I have a set of data that is structured like this: ItemA.GroupA ItemB.GroupA ItemC.GroupB
I have a data set that looks like the following: movie (year) genre for
We have this set of data that we need to get the average of
I have a base Color class that looks something like this. The class is
I have a set of data that has been displayed as just a simple
I have a large set of data that I've collected that I'd like to
I have a set of data that models a hierarchy of categories. A root
I have a set of data that needs to be displayed as a crosstab
I have a set of data that contains garbled text fields because of encoding

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.