[英]Tree Menu Array 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 category = lamps
或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>
有人幫我
這是給你的方法。 此代碼位於您的函數下方。 基本上,我們從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);
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>
但是,這有點冗長:
<?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
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.