如何使用递归操作PHP中的嵌套数组?

如何使用递归操作PHP中的嵌套数组?,php,arrays,Php,Arrays,我想用递归函数或处理嵌套数组的内置函数替换多个foreach操作 我有一个对象来描述数据库中表之间的关系。每个联接表都是添加到“children”属性的类似对象 "dataObject": "job", "data": { "guid": "{{guid}}" [...] }, "children": { "invoice": { "dataObject": "invoice", "data": {

我想用递归函数或处理嵌套数组的内置函数替换多个
foreach
操作

我有一个对象来描述数据库中表之间的关系。每个联接表都是添加到“children”属性的类似对象

"dataObject": "job",
"data": {
    "guid": "{{guid}}"
    [...]
        },
"children": {
    "invoice": {
        "dataObject": "invoice",
        "data": {
            "guid": "{{guid}}"
            [...]
        },
        "children": {
            "payment": {
                "dataObject": "payment",
                "data": {
                    "guid": "{{guid}}"
                    [...]
                },
                "children": {
                    "paymentType": {
                        "dataObject": "paymentType",
                        "data": {
                           "guid": "{{guid}}"
                           [...]
                        },
                        "children": null
                    }
                }
            }
        }
    }
}
我可以使用方法将数组中的数据(PDO结果)添加到对象中

$myObject->set($row);
我想以以下形式返回嵌套数组:

{
"job": {
    "fcfa88e0-af7b-4829-a02a-0a8bb95a2a99": {
        "guid": "fcfa88e0-af7b-4829-a02a-0a8bb95a2a99",
        "jobproperty1": "",
        "jobproperty2": "",
        "invoice": {
            "89ff1cd5-d510-4494-8f39-56a68ebb7879": {
                "guid": "89ff1cd5-d510-4494-8f39-56a68ebb7879",
                "invoiceProperty1": '',
                "invoiceProperty2": '',
                "payment": {
                    "1949c62b-6514-4f3d-85a3-07cd7a2b9f8a": {
                        "guid": 1949c62b-6514-4f3d-85a3-07cd7a2b9f8a,
                        "paymentProperty1": "",
                        "paymentProperty2": "",
                        "paymentType": {
                            "9800184e-e1fd-4a9d-a6ef-0673bb95c38e": {
                                "guid": "9800184e-e1fd-4a9d-a6ef-0673bb95c38e",
                                "title": null,
                                "paid": null,
                            }
                        }
                    }
                }
            }
        }
    }
}
我可以使用多个
foreach
操作将其添加到一个
outputObject
中来实现这一点,但这看起来很可怕

public function children($rootObject, $row){
    $rootObject->set($row);
    $this->outputObject->content[$rootObject->guid] = $rootObject->data;
    foreach($rootObject->children as $child){
        $child->set($row);
        $this->outputObject->content[$rootObject->guid][$child->dataObject][$child->guid] = $child->data;
        foreach($child->children as $grandchild){
            $grandchild->set($row);
            $this->outputObject->content[$rootObject->guid][$child->dataObject][$child->guid][$grandchild->dataObject][$grandchild->guid] = $grandchild->data;
            foreach($grandchild->children as $greatgrandchild){
                $greatgrandchild->set($row);
                $this->outputObject->content[$rootObject->guid][$child->dataObject][$child->guid][$grandchild->dataObject][$grandchild->guid][$greatgrandchild->dataObject][$greatgrandchild->guid] = $greatgrandchild->data;
            }
        }
    }
}
我使用递归函数操作
outputObject
,但结果不是嵌套的。是否有一个内置或自定义功能可以实现这一点

public function childs($thisObject, $row, $path){
    $thisObject->set($row);
    $path = $path[$thisObject->dataObject][$thisObject->guid];
    $this->outputObject->content[$path][$thisObject->guid] = $thisObject->data;
    if(is_array($thisObject->children)){
        foreach($thisObject->children as $child){
            $this->childs($child, $row, $path);
        }
    }
}
递归函数的输出不显示嵌套数组,只显示一个维度

{
"content": {
    "f": {
        "fcfa88e0-af7b-4829-a02a-0a8bb95a2a99": {
            "guid": "fcfa88e0-af7b-4829-a02a-0a8bb95a2a99",
            "jobProperty1": "...",
            "jobProperty2": "..."
        },
        "1949c62b-6514-4f3d-85a3-07cd7a2b9f8a": {
            "guid": "1949c62b-6514-4f3d-85a3-07cd7a2b9f8a",
            "paymentProperty1": "...",
            "paymentProperty2": "..."
        },
        "9800184e-e1fd-4a9d-a6ef-0673bb95c38e": {
            "guid": "9800184e-e1fd-4a9d-a6ef-0673bb95c38e",
            "paymentTypeProperty1": "...",
            "paymentTypeProperty2": "..."
        },
        "89ff1cd5-d510-4494-8f39-56a68ebb7879": {
            "guid": "89ff1cd5-d510-4494-8f39-56a68ebb7879",
            "invoiceProperty1": "...",
            "invoiceProperty2": "..."
        }
    }
}

}

向我们展示您编写的递归函数我更新了我的问题,将递归函数包括在内。当您使用该递归函数时,
$this->outputObject
有什么问题?它只提供了一个包含对象的平面数组。我认为问题在于设置和传递$path变量,但是我不知道如何设置值。向我们展示你编写的递归函数。我更新了我的问题以包含递归函数。当你使用该递归函数时,
$this->outputObject
有什么问题吗?它只给出了一个包含对象的平面数组。我认为问题在于设置和传递$path变量,但我不知道如何设置值。