Home >Backend Development >PHP Tutorial >PHP realizes menu infinite classification

PHP realizes menu infinite classification

肚皮会动
肚皮会动Original
2017-11-14 15:03:393948browse

This article introduces in detail the use of PHP to implement menusInfinitus ClassificationGeneral project menus are displayed differently according to different users and different roles. If you only use if It seems clumsy to judge by else, and it is very troublesome to modify the menu. The best way is to store the menu in the database, then retrieve it from the database, traverse it, and display it on the page. The purpose of this article is to query the data and how to make one-to-one correspondence between the upper and lower levels of the menu. Infinite classification technology is used.

Menu data

Our menu data here is temporary data and is not queried and processed from the database. The data is basically similar to that in the database. The data is as follows:

$items = array(
            1 => array('id' => 1, 'pid' => 0, 'name' => '安徽省'),
            2 => array('id' => 2, 'pid' => 0, 'name' => '浙江省'),
            3 => array('id' => 3, 'pid' => 1, 'name' => '合肥市'),
            4 => array('id' => 4, 'pid' => 3, 'name' => '长丰县'),
            6 => array('id' => 6, 'pid' => 4, 'name' => '小溪村'),
            5 => array('id' => 5, 'pid' => 1, 'name' => '安庆市'),
        );

The data here is also changed to the real data queried from the database.

Create a menu Infinitus classification method

   function generateTree($list, $pk = 'id', $pid = 'pid', $child = '_child', $root = 0)
{
    $tree     = array();
    $packData = array();
    foreach ($list as $data) {
        $packData[$data[$pk]] = $data;
    }
    foreach ($packData as $key => $val) {
        if ($val[$pid] == $root) {
//代表跟节点, 重点一
            $tree[] = &$packData[$key];
        } else {
            //找到其父类,重点二
            $packData[$val[$pid]][$child][] = &$packData[$key];
        }
    }
    return $tree;
}

Analyze the code above: The more important thing here is The comment place in the code, focus 1, focus 2.
1. First traverse the array
2. Focus 1 means to determine whether it is the root menu. If If so, then assign the root menu array and the reference of the array to the new array
4. Point 2 means that the subclass array is assigned to the array with the key $child in the parent class array
5. The final result It is an array of tree structure,

The result is as follows:

Array ( 
[0] => Array ( [id] => 1 [pid] => 0 [name] => 安徽省 [son] => Array ( [0] => Array ( [id] => 3 [pid] => 1 [name] => 合肥市 [son] => Array ( [0] => Array ( [id] => 4 [pid] => 3 [name] => 长丰县 [son] => Array ( [0] => Array ( [id] => 6 [pid] => 4 [name] => 小溪村 ) ) ) ) ) 
[1] => Array ( [id] => 5 [pid] => 1 [name] => 安庆市 ) ) )
 [1] => Array ( [id] => 2 [pid] => 0 [name] => 浙江省 ) )

Return the result to the front end, add the html element, and the effect of the menu will come out

Summary

1. Using PHP to do Infinitus, the difficulty is &, here is not assigning a value, but assigning a reference to an array, so as to operate the same piece of content
2. Reference Assignment is generally used in arrays, Objects, such as php $arr=&$arr1, $CustomerTwo=&$Customer. This does not open up new space in the memory, but changes the memory Pass the reference of this piece of data to the new variable
3. Using this method is much better than using recursion, and it is easier to operate. Recursion has many disadvantages.
4. The above method is not the best , you can find other methods on the Internet. Some methods cause problems after using them, such as the following function:

function generateTree($items){
    $tree = array();
    foreach($items as $item){
        if(isset($items[$item['pid']])){
            $items[$item['pid']]['son'][] = &$items[$item['id']];
        }else{
            $tree[] = &$items[$item['id']];
        }
    }
    return $tree;
}
//测试数据
$items = array(
    1 => array('id' => 1, 'pid' => 0, 'name' => '安徽省'),
    2 => array('id' => 2, 'pid' => 0, 'name' => '浙江省'),
    3 => array('id' => 3, 'pid' => 1, 'name' => '合肥市'),
    4 => array('id' => 4, 'pid' => 3, 'name' => '长丰县'),
    7 => array('id' => 6, 'pid' => 4, 'name' => '小溪村'),
    5 => array('id' => 5, 'pid' => 1, 'name' => '安庆市'),
);

This method can indeed achieve the effect, and there is only one loop, which seems to be very efficient. But it has one of the biggest problems, that is, the id value must be consistent with its corresponding key value. Friends who don't understand can test it by themselves. I hope it will be helpful to you.

Related recommendations:

php development process and sample code for recursively implementing infinite-level classification

php infinite-level classification example code

php Three ways to achieve infinite classification

phpInfinite classification implementation method analysis

The above is the detailed content of PHP realizes menu infinite classification. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn