0

i'm trying to build a json array in php, but a more complex one. The point is, that i'm trying to make a database for dish calculations, and the array should look something like that.

Each dish has a name, a base weight, diameter and height. It also has half-finished products that include a single product with its weight.

For example There's a finished product Called "Cake" they base weight is 750 g., the height is 8cm and the diameter is 8cm. Now this cake is made from 3 different half-products that build up the 750g.:

  1. Dough (300 g.)
  2. Icing (300 g.)
  3. Decorations. (150 g.)

And now every half-product has its own single product that builds it up

Dough: 300g.

  1. Flour 100g
  2. Water 100 g
  3. Something else 100g.

And so on for every half product.

I've tried building arrays but this one seems a bit to hard for me, any suggestions on how todo this? Thanks!

EDIT: I need to get something like this

[
  {
    "Title": "Cake",
    "base_weight": 750,
    "base_height": 8,
    "base_diameter": 14,
    "half_products": [
      {
        "title": "Dough",
        "weight": 300,
        "ingredients": [
          {
            "title": "Flour",
            "Weight": 150
          },
          {
            "title": "Water",
            "Weight": 150
          }
        ]
      },
      {
        "title": "Icing",
        "weight": 300,
        "ingredients": [
          {
            "title": "Flour",
            "Weight": 150
          },
          {
            "title": "Water",
            "Weight": 150
          }
        ]
      },
      {
        "title": "Decorations",
        "weight": 150,
        "ingredients": [
          {
            "title": "Flowers",
            "Weight": 150
          }
        ]
      }
    ]
  }
]
ADyson
  • 57,178
  • 14
  • 51
  • 63
Pakenas
  • 19
  • 4
  • 2
    Seems like it would be an array of arrays, probably. It's unclear where you're stuck - have you attempted anything at all towards this? Also, JSON is largely irrelevant here - you create the data structure within a PHP variable and then just encode it as JSON at the end. If you could show us the JSON you want to end up with (assuming you've thought about that?) then it would be simpler for us. – ADyson Jun 22 '21 at 10:40
  • Thats something close to what i need https://pastebin.com/WTBxKjLg – Pakenas Jun 22 '21 at 10:56
  • Thanks. Yeah it's just an array of arrays. Are you starting from an existing data structure (e.g. from a database or something), or you just want an example of building the array from scratch (i.e. using hard-coded data, just to show the PHP structure)? – ADyson Jun 22 '21 at 11:01
  • Since we're making a calculator to easily determine to amount of ingredients need for a certain sized cake, we will need to create a database and Employees will be able to add the information about the base cakes there through a form, since all of them are different there will be different numbers of half-products and ingredients. But i think a hard-coded example will be good for sure. – Pakenas Jun 22 '21 at 11:06
  • `we will need to create a database and Employees will be able to add the information about the base cakes there through a form,`...so where does JSON come into that? Are you intending to send JSON from the form to PHP? Or output the data from the database in JSON format via an API? Or something else? – ADyson Jun 22 '21 at 11:09
  • The form should format the info into json and then store that in the database. That would be the first step. Then, when we need to calculate something, we would get the array from our db and accordingly calculate all the ingredients needed. Hope this makes sense – Pakenas Jun 22 '21 at 11:11
  • `The form should format the info into json`...so an example of using PHP to generate the JSON is no use to you then, because you'd be doing that in a browser, using Javascript. However you might need to know how to decode the JSON, and separate the items into entries you an insert into a relational database structure...or are you just intending to stick the raw JSON direct into the database in a json column? – ADyson Jun 22 '21 at 11:14
  • Since i'm not really an expert in this area, i don't really now whats the best and most efficient way to do this, but yes, my intention was to to store the array in a single column, and then, when needed decode into seperate entries. – Pakenas Jun 22 '21 at 11:17
  • Whether that's a good idea really depends on the exact usage scenarios and all the requirements for querying, reporting or other manipulation. We don't have all your requirements. Breaking down into a relational structure normally gives more flexibility and ability to write interesting SQL queries on the data. But...newer versions of many database engines do have some JSON-processing capabilities built-in, and/or if you're merely intending to return the JSON as-is when requested, or only do light processing on it in PHP when you retrieve it, then it might be ok to store as JSON. – ADyson Jun 22 '21 at 11:23
  • You really need to consider all your requirements carefully and also do some research, it's too broad a topic to discuss here. – ADyson Jun 22 '21 at 11:23
  • @ADyson is there any way i could contact you privately? It seems that do not meed the requirements to Chat with you – Pakenas Jun 22 '21 at 11:25
  • Unfortunately I am not willing to advertise my private contact details on this public site. However yes it might be a good idea for you to engage with a freelancer of some sort who is more experienced with complex data, you can probably find someone on other sites where you can hire people. – ADyson Jun 22 '21 at 11:26

1 Answers1

0

You can use recursive tree methods for it and then you can easily convert it to json. Arnaud Le Blanc coded it before here https://stackoverflow.com/a/4844073/2528471

function buildTree($items) {
    $childs = array();

    foreach($items as &$item)
        $childs[(int)$item['parent_id']][] = &$item;

    foreach($items as &$item)
        if (isset($childs[$item['id']]))
            $item['childs'] = $childs[$item['id']];

    return $childs[0];
}
$tree = buildTree($items); // $items variable must store all data from your database table

You can use it and then use the standard php method easily.

json_encode($tree);