Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

aurelianspodarec's avatar

Data Structures: When to decide if the data should be flat or nested?

Hello there!

I'm create a pages data, however, I do need to categorize them by: static pages, utility, cms pages etc...

So my question is, should I give them categories by ID or nest them by the category?

This is DEMO UI

/Static Pages 
- Home 
- About 
- Contact
/Utility Pages
- 404 
- Password 
/CMS Pages
- Blog Post Template

So far I got this:

 {
        id: 110,
        projectID: 1,
        name: "About",
        parentFolder: "hi",
        slug: "/about",
        isHome: false,
            accessControl: {
            access: "public",
        },
        seo: {
            titleTag: "",
            metaDescription: ""
        }
    },
    {
        id: 120,
        projectID: 1,
        name: "Contact",
        parentFolder: "hi",
        slug: "/contact",
        isHome: false,
            accessControl: {
            access: "public",
        },
        seo: {
            titleTag: "",
            metaDescription: ""
        }
    },

Do I add category: utility or do I make it like:

{
	'static: {},
   `utility`: {}
}

Not sure which one to choose.

0 likes
2 replies
LaryAI's avatar
Level 58

In general, the decision to use a flat or nested data structure depends on the specific use case and the requirements of the application. In this case, since the pages need to be categorized by type, it would make sense to use a nested data structure.

One possible solution would be to create an object that contains the different categories as keys, and each key would have an array of pages that belong to that category. For example:

{
  "Static Pages": [
    {
      "id": 110,
      "name": "About",
      "slug": "/about",
      "accessControl": {
        "access": "public"
      },
      "seo": {
        "titleTag": "",
        "metaDescription": ""
      }
    },
    {
      "id": 120,
      "name": "Contact",
      "slug": "/contact",
      "accessControl": {
        "access": "public"
      },
      "seo": {
        "titleTag": "",
        "metaDescription": ""
      }
    }
  ],
  "Utility Pages": [
    {
      "id": 130,
      "name": "404",
      "slug": "/404",
      "accessControl": {
        "access": "public"
      },
      "seo": {
        "titleTag": "",
        "metaDescription": ""
      }
    },
    {
      "id": 140,
      "name": "Password",
      "slug": "/password",
      "accessControl": {
        "access": "public"
      },
      "seo": {
        "titleTag": "",
        "metaDescription": ""
      }
    }
  ],
  "CMS Pages": [
    {
      "id": 150,
      "name": "Blog Post Template",
      "slug": "/blog-post-template",
      "accessControl": {
        "access": "public"
      },
      "seo": {
        "titleTag": "",
        "metaDescription": ""
      }
    }
  ]
}

This way, you can easily access the pages for a specific category by using the category name as the key. For example, to get all the pages for the "Static Pages" category, you would use pages["Static Pages"].

Note that this is just one possible solution, and there may be other ways to structure the data depending on the specific requirements of the application.

aurelianspodarec's avatar

@LaryAI Yes AI I know, but what are the PROS and CONS of it? What could be a better use case in my situation?

Please or to participate in this conversation.