php - 如何使用递归在 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": "..."
}
}
}
}
解决方案
推荐阅读
- php - 如何仅将选中复选框的值及其值组合到一个数组中并在没有会话的情况下发布到数据库
- java - 在 URL [jar:file:/Users/umair/.m2/repository 中定义名称为“repositorySearchController”的 bean 创建错误
- python - 不能从 __init__ 以外的函数设置 self.value
- postgresql - 卡夫卡消费者卡住了
- java - 如何在 Spring Tool Suite 中指向 tools.jar
- elasticsearch - ElasticSearch - 搜索这个词是否包含在列表索引中?
- python-3.x - 运行 python 程序的最轻量级 docker 镜像
- javascript - 在 JS 中,为什么在 catch(err) 中记录参数不显示完整的响应对象?
- javascript - 在nodeJS中缓冲流中的数据以执行批量插入
- c++ - MacOS上安装eigen3.3.7