首页 > 解决方案 > 如何使用递归在 PHP 中操作嵌套数组?

问题描述

我想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操作将其添加到 an 中来实现这一点,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": "..."
        }
    }
}

}

标签: phparrays

解决方案


推荐阅读