Home > Mobile >  How to set "parent_id" of child element of multidimensional array from parent id
How to set "parent_id" of child element of multidimensional array from parent id

Time:02-13

I need to display the parent id in the child's array. I have the id of the parent (uuid), now I need to add a new key in the array of the child (parent_id) with the uuid of the parent. I have the following array (PHP):

"array": [
{
    "uuid": 7,
    "nome": "Parent",
    "ativo": 1,
    "childrens": [
        {
            "uuid": 9,
            "nome": "Child",
            "ativo": 1,
            "childrens": [
                {
                    "uuid": 70,
                    "nome": "Child of Child",
                    "ativo": 1,
                    "childrens": [
                        {
                            "uuid": 391,
                            "nome": "Child of Child of Child",
                            "ativo": 1,
                            "childrens": []
                        },
                    ]
                },
            ]
        },
    ]
}

I would like it to be like this:

"array": [
{
    "uuid": 7,
    "nome": "Parent",
    "ativo": 1,
    "childrens": [
        {
            "uuid": 9,
            "parent_id": 7,
            "nome": "Child",
            "ativo": 1,
            "childrens": [
                {
                    "uuid": 70,
                    "parent_id": 9,
                    "nome": "Child of Child",
                    "ativo": 1,
                    "childrens": [
                        {
                            "uuid": 391,
                            "parent_id": 70,
                            "nome": "Child of Child of Child",
                            "ativo": 1,
                            "childrens": []
                        },
                    ]
                },
            ]
        },
    ]
}]

I've tried several ways without success. Thanks in advance for anyone who can help.

CodePudding user response:

$json = '{"array": [
{
    "uuid": 7,
    "nome": "Parent",
    "ativo": 1,
    "childrens": [
        {
            "uuid": 9,
            "nome": "Child",
            "ativo": 1,
            "childrens": [
                {
                    "uuid": 70,
                    "nome": "Child of Child",
                    "ativo": 1,
                    "childrens": [
                        {
                            "uuid": 391,
                            "nome": "Child of Child of Child",
                            "ativo": 1,
                            "childrens": []
                        }
                    ]
                }
            ]
        }
    ]
}]}';
$object = json_decode($json);

foreach($object->array as $elem)
    add_parent($elem->childrens,$elem->uuid);
    
function add_parent($chs,$id){
    foreach($chs as $ch)
    {
        $ch->parent_id = $id;
        if(count($ch->childrens))
            add_parent($ch->childrens,$ch->uuid);
    }
}


var_dump(json_encode($object));

Output:

{
  "array": [
    {
      "uuid": 7,
      "nome": "Parent",
      "ativo": 1,
      "childrens": [
        {
          "uuid": 9,
          "nome": "Child",
          "ativo": 1,
          "childrens": [
            {
              "uuid": 70,
              "nome": "Child of Child",
              "ativo": 1,
              "childrens": [
                {
                  "uuid": 391,
                  "nome": "Child of Child of Child",
                  "ativo": 1,
                  "childrens": [],
                  "parent_id": 70
                }
              ],
              "parent_id": 9
            }
          ],
          "parent_id": 7
        }
      ]
    }
  ]
}
  • Related