php - 合并父子数组
问题描述
我有 PHP 数组。我正在尝试转换为父子数组:我使用了递归函数但没有得到我的输出。
Array
(
[0] => Array
(
[id] => 1
[subscription_name] => Yearly
[parent_id] => 0
)
[1] => Array
(
[id] => 22
[subscription_name] => Yearly new
[parent_id] => 1
)
[2 => Array
(
[id] => 23
[subscription_name] => Yearly new offer
[parent_id] => 22
)
[3] => Array
(
[id] => 24
[subscription_name] => Weekly
[parent_id] => 0
)
[4] => Array
(
[id] => 25
[subscription_name] => Weekly new offer
[parent_id] => 24
)
)
我期待这个结果
Array
(
[0] => Array
(
[id] => 1
[subscription_name] => Yearly new offer
[childrens] => Array
(
[0] => Array
(
[id] => 22
)
[1] => Array
(
[id] => 23
)
)
)
[1] => Array
(
[id] => 24
[subscription_name] => Weekly new offer
[childrens] => Array
(
[0] => Array
(
[id] => 25
)
)
)
)
我已经尝试过但没有得到我想要的输出
我的 PHP 函数是
function tree(array $elements, $parent_id = 0) {
echo "<pre>";
$branch = array();
$a=array();
foreach ($elements as $element) {
if ($element['parent_id'] == $parent_id && $element['subscription_type_id'] !=1 ) {
$children = $this->tree($elements, $element['id']);
if ($children) {
$element['children'] = $children;
}
else {
$element['children'] = array();
}
$branch[] = $element;
}
}
return $branch;
}
这是我从上述函数得到的输出:
Array
(
[0] => Array
(
[id] => 1
[subscription_name] => Yearly
[children] => Array
(
[0] => Array
(
[id] => 22
[subscription_name] => Yearly new
[children] => Array
(
[0] => Array
(
[id] => 23
[subscription_name] => Yearly new offer
[children] => Array
(
)
)
)
)
)
)
[1] => Array
(
[id] => 24
[subscription_name] => Weekly
[children] => Array
(
[0] => Array
(
[id] => 25
[subscription_name] => Weekly new offer
[children] => Array
(
)
)
)
)
请帮我解决这个问题。谢谢。
解决方案
真的,我的意思是非常幼稚的版本,但顺便说一句,你的数据真的更容易被利用,比如parent_id
如果它不是父 ID(例如 NULL)就没有任何到 0 我也不明白你为什么有不同subscription_name
。我认为你应该更多地改变你的输入,而不是找到一个复杂的算法来解析它们
function tree(array $elements) {
$openList = [];
$result = [];
$id = 0;
foreach ($elements as $key => $element) {
if($key != 0) // I suppose the trial one is not used
{
if($element['parent_id'] == 0) // a root
{
$closeList = [];
$openList[$element['id']] = $element['id'];
$result[$id] = $element;
unset($result[$id]['parent_id']);
$result[$id]['children'] = [];
$newOpenlist = [];
while(count($openList) > 0)
{
foreach ($elements as $key => $element) {
if($element['parent_id'] != 0 && in_array($element['parent_id'], $openList) && !in_array($element['parent_id'], $closeList))
{
$newOpenlist[$element['id']] = $element['id'];
$result[$id]['children'][] = $element;
}
}
foreach($openList as $item)
{
$closeList[] = $item;
}
$openList = $newOpenlist;
$newOpenlist = [];
}
}
$id++;
}
}
return $result;
}
推荐阅读
- mysql - 错误代码:1054。“字段列表”中的未知列“喜欢”
- java - 当我自己使用参数字符串抛出异常时,究竟会发生什么?什么时候调用 toString 方法?
- android - 根据月份和年份对多视图类型 RecyclerView 进行排序
- python - 如何使用张量流有效地提取给定长度的所有切片
- jekyll - 如何在包含标签后运行 Jekyll 插件?
- python - 如果另一列数据在列表中,熊猫会更改一列数据
- javascript - 如何使用异步调用完成一系列信息?
- html-framework-7 - 无法在 framework7 中设置堆叠标签
- c++ - 如何在二维数组 ROW 中找到最小值和最大值?
- python - 在 python 中使用 selenium WebDriver 单击表单框中的按钮