1

我很难掌握创建类别树的概念。这是标准:

  • 创建一个无限深度的父/子 HTML 无序列表
  • 删除所有 status = 0 的项目和任何孩子
  • 创建 URL,例如 /clothes/jeans
  • 可选:生成面包屑

我能找到的最接近的解决方案让我更加困惑,因为我无法让它们工作:

这是我的数组:

Array
(
    [0] => Array
        (
            [id] => 1
            [parent] => 0
            [status] => 2
            [slug] => clothes
            [title] => Clothes
        )
    [1] => Array
        (
            [id] => 2
            [parent] => 1
            [status] => 2
            [slug] => jeans
            [title] => Jeans
        )
    [2] => Array
        (
            [id] => 3
            [parent] => 1
            [status] => 2
            [slug] => dresses
            [title] => Dresses
        )
    [3] => Array
        (
            [id] => 4
            [parent] => 0
            [status] => 2
            [slug] => accessories
            [title] => Accessories
        )
    [4] => Array
        (
            [id] => 5
            [parent] => 4
            [status] => 2
            [slug] => bags
            [title] => Bags
        )
    [5] => Array
        (
            [id] => 6
            [parent] => 4
            [status] => 2
            [slug] => watches
            [title] => Watches
        )
    [6] => Array
        (
            [id] => 7
            [parent] => 6
            [status] => 2
            [slug] => rolex
            [title] => Rolex
        )
)

这是我想要的无序列表:

<ul>
    <li><a href="/clothes">Clothes</a>
        <ul>
            <li>
                <a href="/clothes/jeans">Clothes</a>
            </li>
            <li>
                <a href="/clothes/dresses">Clothes</a>
            </li>
        </ul>
    </li>
    <li><a href="/accessories">Accessories</a>
        <ul>
            <li>
                <a href="/accessories/bags">Bags</a>
            </li>
            <li>
                <a href="/accessories/watches">Watches</a>
                <ul>
                    <li>
                        <a href="/accessories/watches/rolex">Rolex</a>
                    </li>
                </ul>
            </li>
        </ul>
    </li>
</ul>
4

2 回答 2

1

也许是这样的递归函数:

function filter_by_parent($parent_id,$ar){
    $retval=array();
    foreach($ar as $a){
        if($a['status']==0)
            continue;
        if($a['parent']==$parent_id)
            $retval[]=$a;
    }
    return $retval;
}

function print_list($parent, $level,$ar,$url_prefix) { 
    $children = filter_by_parent($parent,$ar);
    if(empty($children))
        return;

    echo "<ul>"; 
    foreach($children as $child){ 
        // indent and display the title of this child <br>  
        echo "<li>".$child['title']. "( {$url_prefix}{$child['slug']} )";

        print_list($child['id'], $level+1,$ar,$url_prefix.$child['slug'].'/'); 
        echo  "</li>";
    } 
    echo "</ul>";
}
print_list(0,0,$test,'/');

根据您的输入,结果将是:

  • 衣服( /clothes )
  • -牛仔裤(/衣服/牛仔裤)
  • -连衣裙(/衣服/连衣裙)
  • 配件( /accessories )
  • -包(/附件/包)
  • -手表(/附件/手表)

(这是一个改编版本:Storing Hierarchical Data in a Database

于 2012-11-11T13:50:57.007 回答
0

好的,我用递归函数为你找到了解决方案。如果你赶时间,你可以在这里找到完整的代码。

这篇文章的其余部分只是解释它是如何工作的。

这是你的数组:

$arr = array
(
   0=> array
    (
         'id'  => 1,
         'parent'  => 0,
         'status'  => 2,
         'slug'  => 'clothes',
         'title'  => 'Clothes'
    ),
   1 =>array
    (
         'id'  => 2,
         'parent'  => 1,
         'status'  => 2,
         'slug'  => 'jeans',
         'title'  => 'Jeans'
    ),
   2=> array
    (
         'id'  => 3,
         'parent'  => 1,
         'status'  => 2,
         'slug'  => 'dresses',
         'title'  => 'Dresses'
    ),
    3=> array
    (
         'id'  => 4,
         'parent'  => 0,
         'status'  => 2,
         'slug'  => 'accessories',
         'title'  => 'Accessories'
    ),
    4 => array
    (
         'id'  => 5,
         'parent'  => 4,
         'status'  => 2,
         'slug'  => 'bags',
         'title'  => 'Bags'
    ),
    5 => array
    (
         'id'  => 6,
         'parent'  => 4,
         'status'  => 2,
         'slug'  => 'watches',
         'title'  => 'Watches'
    ),

    6 => array
    (
         'id'  => 7,
         'parent'  => 6,
         'status'  => 2,
         'slug'  => 'rolex',
         'title'  => 'Rolex'
    ) 

) ;

下次在问题中包含数组时,请确保它已准备好在 php 脚本中使用,以便人们可以直接使用它。

现在,首先我通过数组并在父母和孩子之间建立了联系。我所做的是,如果具有 id 的元素1有任何子元素,它的所有子元素都将进入数组的索引 1。这是对我有用的部分。

$tree = array ();
foreach ($arr as $val) {
    $tree [ $val['parent'] ] [] = $val;

}

很简单吧???:)

这是它在此处理后从您的数组生成的数组:

array(4) {
  [0]=>
  array(2) {
    [0]=>
    array(5) {
      ["id"]=>
      int(1)
      ["parent"]=>
      int(0)
      ["status"]=>
      int(2)
      ["slug"]=>
      string(7) "clothes"
      ["title"]=>
      string(7) "Clothes"
    }
    [1]=>
    array(5) {
      ["id"]=>
      int(4)
      ["parent"]=>
      int(0)
      ["status"]=>
      int(2)
      ["slug"]=>
      string(11) "accessories"
      ["title"]=>
      string(11) "Accessories"
    }
  }
  [1]=>
  array(2) {
    [0]=>
    array(5) {
      ["id"]=>
      int(2)
      ["parent"]=>
      int(1)
      ["status"]=>
      int(2)
      ["slug"]=>
      string(5) "jeans"
      ["title"]=>
      string(5) "Jeans"
    }
    [1]=>
    array(5) {
      ["id"]=>
      int(3)
      ["parent"]=>
      int(1)
      ["status"]=>
      int(2)
      ["slug"]=>
      string(7) "dresses"
      ["title"]=>
      string(7) "Dresses"
    }
  }
  [4]=>
  array(2) {
    [0]=>
    array(5) {
      ["id"]=>
      int(5)
      ["parent"]=>
      int(4)
      ["status"]=>
      int(2)
      ["slug"]=>
      string(4) "bags"
      ["title"]=>
      string(4) "Bags"
    }
    [1]=>
    array(5) {
      ["id"]=>
      int(6)
      ["parent"]=>
      int(4)
      ["status"]=>
      int(2)
      ["slug"]=>
      string(7) "watches"
      ["title"]=>
      string(7) "Watches"
    }
  }
  [6]=>
  array(1) {
    [0]=>
    array(5) {
      ["id"]=>
      int(7)
      ["parent"]=>
      int(6)
      ["status"]=>
      int(2)
      ["slug"]=>
      string(5) "rolex"
      ["title"]=>
      string(5) "Rolex"
    }
  }
}

现在,主要部分进入场景。由于任何孩子也可以是某些孩子的父母,因此最简单的解决方案是递归。这是递归部分。

function traverser ($array ,$arr)  {


        if (! $arr)
            return;


    echo "<ul>" . "</br>";


    foreach ($arr as $var ) {
        echo "<li>" . "</br>";
        echo '<a href="/'.$var['slug'].'">'.$var['title'].'</a>';
        if (isset ($array [$var['id'] ]))
            traverser ($array , $array [$var['id'] ] ) ;

        echo "</li>";
    }


    echo "</ul>";

你可以在这里找到完整的代码。

希望有帮助。快乐编码。

于 2012-11-11T23:17:18.550 回答