如何从父id设置多维数组子元素的"parent_id"

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

我需要在子数组中显示父 ID。我有父级的 id (uuid),现在我需要在子级 (parent_id) 的数组中添加一个带有父级 uuid 的新键。 我有以下数组 (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": []
                        },
                    ]
                },
            ]
        },
    ]
}

我希望它是这样的:

"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": []
                        },
                    ]
                },
            ]
        },
    ]
}]

我试了好几种方法都没有成功。在此先感谢任何可以提供帮助的人。

$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));

输出:

{
  "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
        }
      ]
    }
  ]
}