如何在 php 中从平面数组构建分组树数组
How to build grouped tree array from flat array in php
我正在尝试将数组重新格式化为树,主数组和子数组应按“名称”分组 属性。
我的平面阵列就像
$flat = [
['id' => 1, 'parent_id' => 0, 'name' => 'root1'],
['id' => 2, 'parent_id' => 0, 'name' => 'root1'],
['id' => 3, 'parent_id' => 1, 'name' => 'ch-1'],
['id' => 4, 'parent_id' => 1, 'name' => 'ch-1'],
['id' => 5, 'parent_id' => 3, 'name' => 'ch-1-1'],
['id' => 6, 'parent_id' => 3, 'name' => 'ch-1-1'],
['id' => 7, 'parent_id' => 0, 'name' => 'root2'],
['id' => 8, 'parent_id' => 0, 'name' => 'root2'],
['id' => 9, 'parent_id' => 7, 'name' => 'ch3-1'],
['id' => 10, 'parent_id' => 7, 'name' => 'ch3-1']
];
我可以通过
构建树结构
$tree = buildTree($flat, 'parent_id', 'id');
function buildTree(array $flatList)
{
$grouped = [];
foreach ($flatList as $node) {
$grouped[$node['parent_id']][] = $node;
}
$fnBuilder = function ($siblings) use (&$fnBuilder, $grouped) {
foreach ($siblings as $k => $sibling) {
$id = $sibling['id'];
if (isset($grouped[$id])) {
$sibling['children'] = $fnBuilder($grouped[$id]);
}
$siblings[$k] = $sibling;
}
return $siblings;
};
return $fnBuilder($grouped[0]);
}
这非常有效。
但我想要的是构建嵌套数组应该按它的“名称”分组 属性。
所以最终输出应该是这样的
[
"root1": [{
"id": 1,
"parent_id": 0,
"name": "root1",
"children": [
"ch-1": [{
"id": 3,
"parent_id": 1,
"name": "ch-1",
"children": [
"ch-1-1": [{
"id": 5,
"parent_id": 3,
"name": "ch-1-1"
},
{
"id": 6,
"parent_id": 3,
"name": "ch-1-1"
}
]
]
},
{
"id": 4,
"parent_id": 1,
"name": "ch-1"
}
]
]
},
{
"id": 2,
"parent_id": 0,
"name": "root1"
}
],
"root2": [{
"id": 7,
"parent_id": 0,
"name": "root2",
"children": [
"ch3-1": [{
"id": 9,
"parent_id": 7,
"name": "ch3-1"
},
{
"id": 10,
"parent_id": 7,
"name": "ch3-2"
}
]
]
},
{
"id": 8,
"parent_id": 0,
"name": "root2"
}
]
]
我已经卡在这里差不多几天了。请帮我解决这个问题。
谢谢。
在 unset()
中使用这样的函数
function buildTree(array &$flat, $parentId = 0) {
$branch = array();
foreach ($flat as $element) {
if ($element['parent_id'] == $parentId) {
$children = buildTree($flat, $element['id']);
if ($children) {
$element['children'] = $children;
}
$branch[$element['id']] = $element;
unset($flat[$element['id']]);
}
}
return $branch;
}
buildTree 之后我们需要按名称分组这里是我的代码:-
$result = array();
foreach ($buildTree as $element) {
$result[$element['name']][] = $element;
}
希望对您有所帮助。查看来自 here
的详细信息
谢谢
function buildTree(array $flat)
{
$grouped = [];
foreach ($flat as $node) {
$grouped[$node['parent_id']][] = $node;
}
$fnBuilder = function ($siblings) use (&$fnBuilder, $grouped) {
foreach ($siblings as $k => $sibling) {
$id = $sibling['id'];
if (isset($grouped[$id])) {
$sibling['children'] = $fnBuilder($grouped[$id]);
}
$siblings[$k] = $sibling;
}
return $siblings;
};
return $fnBuilder($grouped[0]);
}
$tree = buildTree($flat);
将树结构数组传递给 groupedTree();
$groupd = groupedTree($tree);
echo json_encode($groupd, JSON_PRETTY_PRINT);
function groupedTree($tree)
{
$groupedByFuncTableName = array_reduce($tree, function (array $accumulator, array $element) {
if (isset($element['children'])) {
$element['children'] = groupedTree($element['children']);
}
$accumulator[$element['name']][] = $element;
return $accumulator;
}, []);
return $groupedByFuncTableName;
}
function buildTree2(array $flatList)
{
$groupedchildren = [];
$groupedparents = [];
foreach ($flatList as $node) {
if($node['parent_id'] == 0){
$groupedparents[$node['parent_id']][] = $node;
}else{
$groupedchildren[$node['parent_id']][] = $node;
}
}
$namegroupedparents = [];
foreach ($groupedparents as $parent_group) {
foreach ($parent_group as $node) {
$namegroupedparents[$node['name']][] = $node;
}
}
$namegroupedchildren = [];
foreach ($groupedchildren as $children_group) {
foreach ($children_group as $node) {
$namegroupedchildren[$node['name']][] = $node;
}
}
$fnBuilder = function (&$namegroupedparents) use (&$fnBuilder, $namegroupedchildren) {
foreach($namegroupedparents as &$named){
foreach($named as &$parentgroup){
$id = $parentgroup['id'];
foreach($namegroupedchildren as $thename => $namedall){
foreach($namedall as $childgroup){
if($childgroup['parent_id'] == $id){
if(isset($parentgroup['children'])){
if(!in_array($childgroup, $parentgroup['children'][$thename])){
$parentgroup['children'][$thename][] = $childgroup;
}
}else{
$parentgroup['children'][$thename][] = $childgroup;
}
$fnBuilder($parentgroup['children']);
}
}
}
}
}
return;
};
$fnBuilder($namegroupedparents);
return $namegroupedparents;
}
这适用于您当前的阵列,但我不知道它是否适用于不同的输入。
它按 parent_id
分组,然后按 name
分组,同时将 0
父项与其余子项放在不同的数组中。然后通过递归构建children数组。
我正在尝试将数组重新格式化为树,主数组和子数组应按“名称”分组 属性。
我的平面阵列就像
$flat = [
['id' => 1, 'parent_id' => 0, 'name' => 'root1'],
['id' => 2, 'parent_id' => 0, 'name' => 'root1'],
['id' => 3, 'parent_id' => 1, 'name' => 'ch-1'],
['id' => 4, 'parent_id' => 1, 'name' => 'ch-1'],
['id' => 5, 'parent_id' => 3, 'name' => 'ch-1-1'],
['id' => 6, 'parent_id' => 3, 'name' => 'ch-1-1'],
['id' => 7, 'parent_id' => 0, 'name' => 'root2'],
['id' => 8, 'parent_id' => 0, 'name' => 'root2'],
['id' => 9, 'parent_id' => 7, 'name' => 'ch3-1'],
['id' => 10, 'parent_id' => 7, 'name' => 'ch3-1']
];
我可以通过
构建树结构$tree = buildTree($flat, 'parent_id', 'id');
function buildTree(array $flatList)
{
$grouped = [];
foreach ($flatList as $node) {
$grouped[$node['parent_id']][] = $node;
}
$fnBuilder = function ($siblings) use (&$fnBuilder, $grouped) {
foreach ($siblings as $k => $sibling) {
$id = $sibling['id'];
if (isset($grouped[$id])) {
$sibling['children'] = $fnBuilder($grouped[$id]);
}
$siblings[$k] = $sibling;
}
return $siblings;
};
return $fnBuilder($grouped[0]);
}
这非常有效。
但我想要的是构建嵌套数组应该按它的“名称”分组 属性。
所以最终输出应该是这样的
[
"root1": [{
"id": 1,
"parent_id": 0,
"name": "root1",
"children": [
"ch-1": [{
"id": 3,
"parent_id": 1,
"name": "ch-1",
"children": [
"ch-1-1": [{
"id": 5,
"parent_id": 3,
"name": "ch-1-1"
},
{
"id": 6,
"parent_id": 3,
"name": "ch-1-1"
}
]
]
},
{
"id": 4,
"parent_id": 1,
"name": "ch-1"
}
]
]
},
{
"id": 2,
"parent_id": 0,
"name": "root1"
}
],
"root2": [{
"id": 7,
"parent_id": 0,
"name": "root2",
"children": [
"ch3-1": [{
"id": 9,
"parent_id": 7,
"name": "ch3-1"
},
{
"id": 10,
"parent_id": 7,
"name": "ch3-2"
}
]
]
},
{
"id": 8,
"parent_id": 0,
"name": "root2"
}
]
]
我已经卡在这里差不多几天了。请帮我解决这个问题。 谢谢。
在 unset()
function buildTree(array &$flat, $parentId = 0) {
$branch = array();
foreach ($flat as $element) {
if ($element['parent_id'] == $parentId) {
$children = buildTree($flat, $element['id']);
if ($children) {
$element['children'] = $children;
}
$branch[$element['id']] = $element;
unset($flat[$element['id']]);
}
}
return $branch;
}
buildTree 之后我们需要按名称分组这里是我的代码:-
$result = array();
foreach ($buildTree as $element) {
$result[$element['name']][] = $element;
}
希望对您有所帮助。查看来自 here
的详细信息谢谢
function buildTree(array $flat)
{
$grouped = [];
foreach ($flat as $node) {
$grouped[$node['parent_id']][] = $node;
}
$fnBuilder = function ($siblings) use (&$fnBuilder, $grouped) {
foreach ($siblings as $k => $sibling) {
$id = $sibling['id'];
if (isset($grouped[$id])) {
$sibling['children'] = $fnBuilder($grouped[$id]);
}
$siblings[$k] = $sibling;
}
return $siblings;
};
return $fnBuilder($grouped[0]);
}
$tree = buildTree($flat);
将树结构数组传递给 groupedTree();
$groupd = groupedTree($tree);
echo json_encode($groupd, JSON_PRETTY_PRINT);
function groupedTree($tree)
{
$groupedByFuncTableName = array_reduce($tree, function (array $accumulator, array $element) {
if (isset($element['children'])) {
$element['children'] = groupedTree($element['children']);
}
$accumulator[$element['name']][] = $element;
return $accumulator;
}, []);
return $groupedByFuncTableName;
}
function buildTree2(array $flatList)
{
$groupedchildren = [];
$groupedparents = [];
foreach ($flatList as $node) {
if($node['parent_id'] == 0){
$groupedparents[$node['parent_id']][] = $node;
}else{
$groupedchildren[$node['parent_id']][] = $node;
}
}
$namegroupedparents = [];
foreach ($groupedparents as $parent_group) {
foreach ($parent_group as $node) {
$namegroupedparents[$node['name']][] = $node;
}
}
$namegroupedchildren = [];
foreach ($groupedchildren as $children_group) {
foreach ($children_group as $node) {
$namegroupedchildren[$node['name']][] = $node;
}
}
$fnBuilder = function (&$namegroupedparents) use (&$fnBuilder, $namegroupedchildren) {
foreach($namegroupedparents as &$named){
foreach($named as &$parentgroup){
$id = $parentgroup['id'];
foreach($namegroupedchildren as $thename => $namedall){
foreach($namedall as $childgroup){
if($childgroup['parent_id'] == $id){
if(isset($parentgroup['children'])){
if(!in_array($childgroup, $parentgroup['children'][$thename])){
$parentgroup['children'][$thename][] = $childgroup;
}
}else{
$parentgroup['children'][$thename][] = $childgroup;
}
$fnBuilder($parentgroup['children']);
}
}
}
}
}
return;
};
$fnBuilder($namegroupedparents);
return $namegroupedparents;
}
这适用于您当前的阵列,但我不知道它是否适用于不同的输入。
它按 parent_id
分组,然后按 name
分组,同时将 0
父项与其余子项放在不同的数组中。然后通过递归构建children数组。