2014-03-13 82 views
0

我解決不了這個問題樹形菜單陣列PHP

Array 
(
    [0] => Array 
     (
      [id] => 1 
      [menu] => shoes 
      [anchor] => Shoes 
      [parent] => 0 
     ) 

    [1] => Array 
     (
      [id] => 2 
      [menu] => futsal-shoes 
      [anchor] => Futsal Shoes 
      [parent] => 1 
     ) 

    [2] => Array 
     (
      [id] => 3 
      [menu] => lamps 
      [anchor] => Lamps 
      [parent] => 0 
     ) 

    [3] => Array 
     (
      [id] => 4 
      [menu] => desk-lamps 
      [anchor] => Desk Lamps 
      [parent] => 3 
     ) 

    [4] => Array 
     (
      [id] => 5 
      [menu] => floor-lamps 
      [anchor] => Floor Lamps 
      [parent] => 3 
     ) 

    [5] => Array 
     (
      [id] => 6 
      [menu] => swing-arm-lamps 
      [anchor] => Swing Arm Lamps 
      [parent] => 4 
     ) 

) 

該功能顯示所有陣列

function has_children($rows,$id) { 
    foreach ($rows as $row) { 
     if ($row['parent'] == $id) 
     return true; 
    } 
    return false; 
} 
function build_menu($rows,$parent=0) { 
    $result = "<ul>"; 
    foreach ($rows as $row) { 
     if ($row['parent'] == $parent) { 
      $result .= "<li><a href=\"$row[menu]\">$row[anchor]</a>"; 
     if (has_children($rows,$row['id'])) 
      $result.= build_menu($rows,$row['id']); 
      $result .= "</li>"; 
     } 
    } 
    $result.= "</ul>"; 
    return $result; 
} 
echo build_menu($array); 

我需要從

只顯示相關的樹狀菜單,如果GET類別=燈

或GET子類別=落地燈

或GET sub_subcategory =搖臂燈

他們只顯示相關的(不是所有的數組)

<ul> 
    <li><a href="">Lamps</a> 
    <ul> 
     <li><a href="">Desk Lamps</a></li> 
     <ul> 
      <li><a href="">Swing Arm Lamps</a></li> 
     </ul> 
     <li><a href="">Floor Lamps</a></li> 
    </ul> 
</ul> 

任何人幫助我。

回答

0

以下是您的一種方法。這段代碼在你的函數下面。基本上我們從GET數組中獲得一個變量並將其賦值給$ current_menu。然後,我們使用foreach來獲取特定菜單項的id(如果數組的鍵是['menu']項,這會更簡單),並將其分配給$ parent。最後,我們將$ parent傳遞給build_menus函數。

//A terse way to do it, instead of typing out all those GETs 
//NOTE: This assumes you'll only have one of these at a time. 
$cats = array("category", "subcategory", "sub_subcategory"); 
foreach ($cats as $cat) { 
    if (isset($_GET[$cat])) { 
     $current_menu = $_GET[$cat];  
    } 
} 

foreach ($array as $arr) { 
    if ($current_menu == $arr['menu']) { 
     $parent = $arr['parent']; 
    } 
} 

echo build_menu($array, $parent); 
+0

謝謝回答larsAnders,我已經試過你的代碼 這項工作 GET落地燈及獲得搖臂燈 但不包括燈 當我嘗試GET燈,該代碼顯示所有的數組 我將使用此原則再次嘗試 – Tsan

+0

在您的數組中,燈的父級設置爲0,因此它將顯示整個數組。如果更改數組中的父項,則它將顯示較少。 – larsAnders

0

SQL:

CREATE TABLE `thematic_tree` (
    `id` int(11) unsigned NOT NULL AUTO_INCREMENT, 
    `parent_id` int(11) unsigned NOT NULL, 
    `name` varchar(50) NOT NULL, 
    `description` varchar(1000) DEFAULT NULL, 
    PRIMARY KEY (`id`) 
) ENGINE=InnoDB AUTO_INCREMENT=28 DEFAULT CHARSET=utf8; 

PHP:

class Thematic { 

private $thematics; 

public function __construct($database) { 

    $this->database = $database; 
    $this->thematics = $this->Init(); 
} 

private function Init() { 
    $result = []; 
    $SQL = "SELECT * FROM thematic_tree;"; 
    if ($res = $this->database->query($SQL)) { 
     while ($row = $res->fetch_assoc()) $result[] = $row; 
     $res->close(); 
    } 
    return $result; 
} 

private function createTree(&$list, $parent){ 
    $tree = array(); 
    foreach ($parent as $k=>$l){ 
     if(isset($list[$l['id']])){ 
      $l['childs'] = $this->createTree($list, $list[$l['id']]); 
     } 
     $tree[] = $l; 
    } 
    return $tree; 
} 

public function getTree(){ 
    $new = array(); 
    foreach ($this->thematics as $a){ 
     $new[$a['parent_id']][] = $a; 
    } 
    return $this->createTree($new, $new[0]); 
} 

嫩枝:

 {% macro menu_links(thematics, base_path) %} 
     {% for item in thematics %} 
      <li> 
       <a href="{{ base_path }}/thematic/{{ item.id }}">{{ item.name }}</a> 
       {% if item.childs %} 
        <ul> 
         {{ _self.menu_links(item.childs, base_path) }} 
        </ul> 
       {% endif %} 
      </li> 
     {% endfor %} 
    {% endmacro %} 

    {% import _self as macros %} 
    <ul class="main-menu"> 
     {{ macros.menu_links(thematics, base_path) }} 
    </ul> 
0

這是一個有點冗長,但:

<?php 

$flat = array(
    array('id' => 1, 'parent' => 0, 'menu' => 'shoes', 'anchor' => 'Shoes'), 
    array('id' => 2, 'parent' => 1, 'menu' => 'futsal-shoes', 'anchor' => 'Futsal Shoes'), 
    array('id' => 3, 'parent' => 0, 'menu' => 'lamps', 'anchor' => 'Lamps'), 
    array('id' => 4, 'parent' => 3, 'menu' => 'desk-lamps', 'anchor' => 'Desk Lamps'), 
    array('id' => 5, 'parent' => 3, 'menu' => 'floor-lamps', 'anchor' => 'Floor Lamps'), 
    array('id' => 6, 'parent' => 4, 'menu' => 'swing-arm-lamps', 'anchor' => 'Swing Arm Lamps'), 
); 

$root = array(
    'children' => array_tree_expand($flat, 'id', 'parent') 
); 

$query = array('menu' => 'shoes'); 

if (empty($query)) { 
    build_menu_children($root['children']); 
} 
else { 
    $item = array_tree_find($root, array('menu' => 'shoes')); 
    if ($item) { 
     build_menu($item); 
    } 
    else { 
     echo 'error: not found'; 
    } 
} 
echo "\n"; 

function build_menu($root) 
{ 
    echo '<ul><li>'; 
    echo sprintf('<a href="%s">%s</a>', htmlspecialchars($root['menu']), htmlspecialchars($root['anchor'])); 
    if ($root['children']) { 
     build_menu_children($root['children']); 
    } 
    echo '</li></ul>'; 
} 

function build_menu_children(array $children) 
{ 
    echo '<ul>'; 
    foreach ($children as $child) { 
     echo '<li>'; 
     echo sprintf('<a href="%s">%s</a>', htmlspecialchars($child['menu']), htmlspecialchars($child['anchor'])); 
     if (count($child['children'])) { 
      build_menu_children($child['children']); 
     } 
     echo '</li>'; 
    } 
    echo '</ul>'; 
} 

function array_tree_find(array $root, array $query) 
{ 
    $match = true; 
    foreach ($query as $k => $v) { 
     if (!array_key_exists($k, $root)) { 
      $match = false; 
      break; 
     } 
     if ($root[$k] !== $query[$k]) { 
      $match = false; 
      break; 
     } 
    } 
    if ($match) { 
     return $root; 
    } 

    foreach ($root['children'] as $child) { 
     $found = array_tree_find($child, $query); 
     if ($found !== null) { 
      return $found; 
     } 
    } 

    return null; 
} 

// http://stackoverflow.com/a/25478474 
function array_tree_expand(array $array, $id = 'id', $parent = 'pid', $children = 'children') 
{ 
    $r = array(); 
    foreach ($array as $v) { 
     $k = $v[$id]; 
     $r[$k] = $v; 
     $r[$k][$children] = array(); 
    } 
    $adopted = array(); 
    foreach ($r as $k => $v) { 
     if (isset($r[$v[$parent]])) { 
      $r[$v[$parent]][$children][] = &$r[$k]; 
      $adopted[] = $k; 
     } 
    } 
    foreach ($adopted as $id) { 
     unset($r[$id]); 
    } 
    return $r; 
} 

function array_tree_flatten(array $tree, $children = 'children', $level = 'level') 
{ 
    $spec = compact('children', 'level'); 
    $ret = array(); 
    array_tree_flatten_walk($ret, $tree, $spec); 
    return $ret; 
} 

function array_tree_flatten_walk(array &$ret, array $tree, array $spec, $level = 0) 
{ 
    foreach ($tree as $child) { 
     $children = $child[$spec['children']]; 
     unset($child[$spec['children']]); 
     $child[$spec['level']] = $level; 
     $ret[] = $child; 
     array_tree_flatten_walk($ret, $children, $spec, $level + 1); 
    } 
} 

您可以保存到a.php和運行php a.php