将树转换为PHP中的完整路径列表

时间:2018-10-04 12:29:45

标签: php recursion tree

我正在尝试将树结构转换为路径列表。

树看起来像这样:

array (
  0 => 
  array (
    'name' => 'Default Category',
    'children' => 
    array (
      0 => 
      array (
        'name' => 'Carpet',
        'children' => 
        array (
          0 => 
          array (
            'name' => 'Colour',
            'children' => 
            array (
              0 => 
              array (
                'name' => 'White',
                'children' => 
                array (
                ),
              ),
              1 => 
              array (
                'name' => 'Grey',
                'children' => 
                array (
                ),
              ),
              2 => 
              array (
                'name' => 'Black',
                'children' => 
                array (
                ),
              ),
              3 => 
              array (
                'name' => 'Light',
                'children' => 
                array (
                ),
              ),
              4 => 
              array (
                'name' => 'Medium',
                'children' => 
                array (
                ),
              ),
              5 => 
              array (
                'name' => 'Dark',
                'children' => 
                array (
                ),
              ),
            ),
          ),
          1 => 
          array (
            'name' => 'Style',
            'children' => 
            array (
              0 => 
              array (
                'name' => 'Twist Pile Carpets',
                'children' => 
                array (
                ),
              ),
              1 => 
              array (
                'name' => 'Stainfree Carpets',
                'children' => 
                array (
                ),
              ),
              2 => 
              array (
                'name' => 'Saxony and Shagpile Carpets',
                'children' => 
                array (
                ),
              ),
              3 => 
              array (
                'name' => 'Soft Carpets',
                'children' => 
                array (
                ),
              ),
              4 => 
              array (
                'name' => 'Loop Pile & Berber Carpets',
                'children' => 
                array (
                ),
              ),
              5 => 
              array (
                'name' => 'Wool Carpets',
                'children' => 
                array (
                ),
              ),
              6 => 
              array (
                'name' => 'Striped & Patterned Carpet',
                'children' => 
                array (
                ),
              ),
              7 => 
              array (
                'name' => 'Natural Carpets',
                'children' => 
                array (
                ),
              ),
            ),
          ),

我的最终结果将是一个看起来像这样的数组:

Default Category/
Default Category/Carpet
Default Category/Carpet/Colour
Default Category/Carpet/Colour/White
Default Category/Carpet/Colour/Grey
Default Category/Carpet/Colour/Black
...
Default Category/Carpet/Style/
Default Category/Carpet/Style/Twist Pile Carpets
Default Category/Carpet/Style/Stainfree Carpets

我知道我需要在此处使用递归,并且可以对树进行迭代,但是我不确定在遍历树时如何构建路径列表。

我尝试搜索,但是没有找到与我特别需要的东西相近的东西,我似乎有一种印象,就是我正在使用的树是多向树,要解决的问题略有不同。 >

一些建议是为了简化树,但是我已经通过删除其他不必要的键来做到这一点,并且我已经看到建议将数组展平以简化遍历过程,但是我不清楚这将如何工作

任何帮助将不胜感激。

更新2

这是我目前拥有的东西,它适用于第一个路径,即:默认类别/地毯/颜色/白色,但是第二个路径不会包含默认类别/地毯/颜色/以上,这最终就是我的意思试图弄清楚该怎么做。

function generateCategoryPaths($categoryTree, &$paths = null, &$path = null)
{
    if (! $paths) {
        $paths = [];
    }

    foreach ($categoryTree as $category) {
        $path .= $category['name'];
        if (isset($category['children']) && count($category['children']) > 0) {
            $path .= '/';
            generateCategoryPaths($category['children'], $paths, $path, $level);
            continue;
        }

        $paths[] = $path;
        $path = '';
    }

    return $paths;
}

并调用代码:

$categoryPaths = generateCategoryPaths($categories);

这将返回以下内容:

array (
  0 => 'Default Category/Carpet/Colour/White',
  1 => 'Grey',
  2 => 'Black',
  3 => 'Light',
  4 => 'Medium',
  5 => 'Dark',
  6 => 'Style/Twist Pile Carpets',
  7 => 'Stainfree Carpets',
  8 => 'Saxony and Shagpile Carpets',
  9 => 'Soft Carpets',
  10 => 'Loop Pile & Berber Carpets',
  11 => 'Wool Carpets',
  12 => 'Striped & Patterned Carpet',
  13 => 'Natural Carpets',
  14 => 'Brand',
  15 => 'Room/Bedroom Carpet',
  16 => 'Living Room Carpet',
  17 => 'Hall, Stairs & Landing Carpet',
  18 => 'Office Carpet',
  19 => 'Conservatory Carpet',
  20 => 'Dining Room Carpet',
  21 => 'Whole House',
  22 => 'Vinyl/Colour/White',
  23 => 'Grey',
  24 => 'Black',
  25 => 'Light',
  26 => 'Medium',
  27 => 'Dark',
  28 => 'Style/Wood Effect Luxury Vinyl Flooring',
  29 => 'Tile Effect Luxury Vinyl Flooring',
  30 => 'Parquet Luxury Vinyl Flooring',
  31 => 'Plain',
  32 => 'Brand/Naturelle',
  33 => 'TLC',
  34 => 'Karndean Flooring',
  35 => 'Polyflor Flooring',
  36 => 'Amtico',
  37 => 'Quickstep Livyn',
  38 => 'COREtec Flooring',
  39 => 'Installation/Click Vinyl Flooring',
  40 => 'Stick Down Vinyl Flooring',
  41 => 'Laminate/Colour/White',

第一行是我试图为每个条目复制它的地方,它显示了穿过树的路径。

第二行和后续行将丢弃当前路径,这就是我要解决的问题。

更新3

输入树的JSON编码版本:

[{"name":"Default Category","level":"1","children":[{"name":"Carpet","level":"2","children":[{"name":"Colour","level":"3","children":[{"name":"White","level":"4","children":[]},{"name":"Grey","level":"4","children":[]},{"name":"Black","level":"4","children":[]},{"name":"Light","level":"4","children":[]},{"name":"Medium","level":"4","children":[]},{"name":"Dark","level":"4","children":[]}]},{"name":"Style","level":"3","children":[{"name":"Twist Pile Carpets","level":"4","children":[]},{"name":"Stainfree Carpets","level":"4","children":[]},{"name":"Saxony and Shagpile Carpets","level":"4","children":[]},{"name":"Soft Carpets","level":"4","children":[]},{"name":"Loop Pile & Berber Carpets","level":"4","children":[]},{"name":"Wool Carpets","level":"4","children":[]},{"name":"Striped & Patterned Carpet","level":"4","children":[]},{"name":"Natural Carpets","level":"4","children":[]}]}]}]}]

2 个答案:

答案 0 :(得分:0)

我创建了一个树生成器,该树生成器使用<ul>'s创建了一个<li>'s,其中可能包含其他<ul>'s<li>'s。您可以在https://delboy1978uk.wordpress.com/2014/12/06/tree-generator-for-multi-dimensional-arrays/

上阅读我的博客文章。

这并不是您真正要寻找的东西,但是调整此类以生成斜线而不是打开/关闭标签应该并不难!重新部署策略很重要!祝你好运!

class Tree
{
    private $data;
    private $html;

    public function buildTree(array $data, $parent_id)
    {
        $this->data = $data;
        $this->html = '<ul>';
        $this->getChildren($parent_id);
        $this->html .= '</ul>';
        return $this->html;
    }

    private function getChildren($parentId)
    {
        $children = array();
        foreach ($this->data as $row) {
            if ($row['parentid'] == $parentId) {
                $children[] = $row;
            }
        }
        foreach($children as $child)
        { 
            $this->html .= '<li>'.$child['name'].'<ul>';
            $this->getChildren($child['id']); 
            $this->html .= '</ul></li>';
        } 

    }
}


$tree = new Tree();
$ul = $tree->buildTree($data, 0);
echo $ul;

答案 1 :(得分:0)

我建议将递归与参考变量一起使用。用此符号“&”定义的参考变量使php变量异步工作,这意味着无论定义在何处,定义都是统一的。

<?php
function listPaths(array $array=[], string $parent="", array &$names=[]):array
{

   foreach($array as $data){

     if(isset($data['name'])){
          $name = $parent."/".$data['name'];
          $names[] = $name;

        if(isset($data['children'])){
           listPaths($data['children'],$name,$names);
        }
     }
  }

    return $names;
}

$result = listPaths($array);