从我的数组树中添加前缀“>”
Adding prefix '>' from my array tree
Array
(
[0] => Array
(
[id] => 39
[parent_id] => 0
[sku] => Parent
)
[1] => Array
(
[id] => 40
[parent_id] => 39
[sku] => Child of parent id 39
)
[2] => Array
(
[id] => 41
[parent_id] => 40
[sku] => Child of child id 40
)
[3] => Array
(
[id] => 42
[parent_id] => 40
[sku] => Another child of child id 40
)
[4] => Array
(
[id] => 43
[parent_id] => 39
[sku] => Another child of parent id 39
)
etc..
)
我这里有一个关联数组,它基本上是 parent - child 关系。网上有很多关于如何构建树的示例,对我帮助很大。但现在我只想将我的数组格式化为这样的格式:
> Parent --> first prefix ">"
>> Child of parent id 39 --> added two ">" prefixes
>>> Child of child id 40 --> added three ">" prefixes
>>> Another child of child id 40
>> Another child of parent id 39
and so on..
更新:
这就是我现在构建树的方式:
function pdf_content($data, $parentId = 0)
{
$str = '';
foreach ($data as $row) {
if ($row['parent_id'] == $parentId) {
$str .= '<li>';
$str .= $row['sku'];
$str .= pdf_content($data, $row['id']);
$str .= '</li>';
}
}
if ($str) {
$str = "<ol class='dashed'>" . $str . '</ol>';
}
return $str;
}
// this is using the ol list style.
所以首先 children 必须得到它的 parent 并附加前缀“>”。我已经有了构建树的代码,但是将它格式化为这样的东西让我卡在树上。
要添加前缀,请使用带有传递参数 $prefix = '>'
的递归函数,如果找到子项,则连接 $prefix .= '>'
,如果未找到子项,则传入您的函数,然后仅覆盖 $prefix = '>'
我认为应该这样做。
function pdf_content($items, $parentId = 0, $prefix = '>')
{
$str = '';
foreach ($items as $item)
{
if ($item['parent_id'] == $parentId) {
$str .= '<li>';
$str .= $prefix.$item['sku'];
$str .= pdf_content($items, $item['id'], $prefix.'>');
$str .= '</li>';
}
}
if ($str) {
$str = "<ol class='dashed'>" . $str . '</ol>';
}
return $str;
}
$string = pdf_content($array);
这就是我得到的输出 -
>Parent
>>Parent 39
>>>Parent 40
>>>Parent 40
>>Parent 39
Array
(
[0] => Array
(
[id] => 39
[parent_id] => 0
[sku] => Parent
)
[1] => Array
(
[id] => 40
[parent_id] => 39
[sku] => Child of parent id 39
)
[2] => Array
(
[id] => 41
[parent_id] => 40
[sku] => Child of child id 40
)
[3] => Array
(
[id] => 42
[parent_id] => 40
[sku] => Another child of child id 40
)
[4] => Array
(
[id] => 43
[parent_id] => 39
[sku] => Another child of parent id 39
)
etc..
)
我这里有一个关联数组,它基本上是 parent - child 关系。网上有很多关于如何构建树的示例,对我帮助很大。但现在我只想将我的数组格式化为这样的格式:
> Parent --> first prefix ">"
>> Child of parent id 39 --> added two ">" prefixes
>>> Child of child id 40 --> added three ">" prefixes
>>> Another child of child id 40
>> Another child of parent id 39
and so on..
更新: 这就是我现在构建树的方式:
function pdf_content($data, $parentId = 0)
{
$str = '';
foreach ($data as $row) {
if ($row['parent_id'] == $parentId) {
$str .= '<li>';
$str .= $row['sku'];
$str .= pdf_content($data, $row['id']);
$str .= '</li>';
}
}
if ($str) {
$str = "<ol class='dashed'>" . $str . '</ol>';
}
return $str;
}
// this is using the ol list style.
所以首先 children 必须得到它的 parent 并附加前缀“>”。我已经有了构建树的代码,但是将它格式化为这样的东西让我卡在树上。
要添加前缀,请使用带有传递参数 $prefix = '>'
的递归函数,如果找到子项,则连接 $prefix .= '>'
,如果未找到子项,则传入您的函数,然后仅覆盖 $prefix = '>'
我认为应该这样做。
function pdf_content($items, $parentId = 0, $prefix = '>')
{
$str = '';
foreach ($items as $item)
{
if ($item['parent_id'] == $parentId) {
$str .= '<li>';
$str .= $prefix.$item['sku'];
$str .= pdf_content($items, $item['id'], $prefix.'>');
$str .= '</li>';
}
}
if ($str) {
$str = "<ol class='dashed'>" . $str . '</ol>';
}
return $str;
}
$string = pdf_content($array);
这就是我得到的输出 -
>Parent
>>Parent 39
>>>Parent 40
>>>Parent 40
>>Parent 39