问题描述
我的模型上有亲子关系.我想要的是将所有这些集合(包括所有父代的后代)展平.
I have a parent children relationship on my model. What I want is to flatten all those collection including all those descendants of parent.
[
{
'name': 'Parent',
'another_array' => [
{
'name': 'Another Array Name'
}
],
'children': [
{
'name': 'First Child',
'address': 'lorem ipsum',
'contact_no': '92390',
'another_array' => [
{
'name': 'Another Array Name'
}
],
'children': [
{
'name': 'First Descendant',
'address': 'lorem ipsum',
'contact_no': '92390',
'children': [
{
'name': 'Descendant Child',
'address': 'lorem ipsum',
'contact_no': '92390',
'children': [
{
'name': 'Last Descendant',
'address': '',
'contact_no': '',
'children': []
}
]
}
]
}
]
}
]
}
]
我尝试了以下代码.但是,它仅显示子级的第二深度.它不会显示该集合的下一个子代.
I tried the code below. However it only displays the second depth of children. It does not display the next children of the collection.
User.php
public function parent()
{
return $this->belongsTo(User::class, 'id', 'parent_id');
}
public function children()
{
return $this->hasMany(User::class,'parent_id', 'id');
}
public function descendants()
{
return $this->children()->with('descendants');
}
UsersController.php
public function index()
{
$parent = User::find(1);
$parent->with('descendants')->find($parent->id);
$descendants = $this->traverseTree($parent->descendants);
return response($descendants);
}
protected function traverseTree($subtree)
{
$descendants = collect([]);
foreach ($subtree->descendants as $descendant) {
$descendants->push($descendant);
if($descendant->descendants instanceof Collection) {
foreach ($descendant->descendants as $node) {
$this->traverseTree($node);
$descendants->push($node);
}
}
}
return $descendants;
}
所需的输出
[
{
'name': 'Parent',
'address': 'lorem ipsum',
'contact_no': '92390'
},
{
'name': 'First Child',
'address': 'lorem ipsum',
'contact_no': '92390'
},
{
'name': 'First Descendant',
'address': 'lorem ipsum',
'contact_no': '92390',
},
{
'name': 'Descendant Child',
'address': 'lorem ipsum',
'contact_no': '92390',
},
{
'name': 'Last Descendant' ,
'address': '',
'contact_no': '',
}
]
期待您的帮助.
推荐答案
通过链接 toArray()
方法,将其转换为数组,以便输出并成为一个多维数组而不是如下所示的嵌套对象...
from your collection response convert it to an array by chaining the toArray()
method so you output becomes and a multi-dimensional array instead of nested objects as shown below...
$my_array = [
'name' => 'Parent',
'children' => [
'name' => 'First Child',
'address' => 'lorem ipsum',
'contact_no' => '234',
'children' => [
'name' => 'First Descendant',
'address' => 'lorem ipsum',
'contact_no' => '567',
'children' => [
'name' => 'Descendant Child',
'address' => 'lorem ipsum',
'contact_no' => '8890',
'children' => [
'name' => 'Last Descendant',
'address' => '',
'contact_no' => '',
'children' => []
]
]
]
]
];
然后您可以在下面使用此帮助功能:
then you can use this helper function below:
protected function _flattened($array)
{
$flatArray = [];
if (!is_array($array)) {
$array = (array)$array;
}
foreach($array as $key => $value) {
if (is_array($value) || is_object($value)) {
$flatArray = array_merge($flatArray, $this->_flattened($value));
} else {
$flatArray[0][$key] = $value;
}
}
return $flatArray;
}
当您转储 $ result = $ this-> _flattened($ my_array);
时,您将得到类似...的输出
when you dump the $result = $this->_flattened($my_array);
, you will have output like...
[
0 => [
"name" => "Parent"
],
1 => [
"name" => "First Child",
"address" => "lorem ipsum"
"contact_no" => "234"
],
2 => [
"name" => "First Descendant"
"address" => "lorem ipsum"
"contact_no" => "567"
],
3 => [
"name" => "Descendant Child"
"address" => "lorem ipsum"
"contact_no" => "8890"
],
4 => [
"name" => "Last Descendant"
"address" => ""
"contact_no" => ""
],
];
这篇关于扁平幼虫嵌套关系(后代父母)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!