PHP efficient retrieval of tree structure information

Source: Internet
Author: User
PHP efficiently obtains tree structure information & nbsp; during development, there are often some simple tree structure storage, such as product multi-level classification, multi-level navigation menu, etc. these objects have a feature, that is, the entire tree structure is usually obtained. There are three methods to save the tree structure in the database. the simplest one is to use a father_id to point to the parent section of the current structure. PHP can efficiently obtain the tree structure information.
During development, there are often some simple tree-like storage, such as multi-level product classification and multi-level navigation menu. these objects have a feature that they usually obtain the entire tree structure. There are three methods to save the tree structure in the database. the simplest one is to point to the parent node of the current structure through a father_id. For such a small structure, we generally use the simplest storage method.
To obtain the entire tree structure, recursive methods are generally used on the internet. the code is easy to understand, but the disadvantage is that you need to perform multiple database queries, in addition, the number of elements obtained in the next few times is very small or even 0, and the efficiency is very poor.
In fact, in this case, you can obtain all the elements from the database, and then build a tree structure based on the obtained element father_id, which can solve the performance loss of executing multiple database queries. The following describes how to obtain the functions in the multi-level navigation bar menu in my actual project.
/* Obtain all elements from the database and construct a tree structure using the following function */private function getTree ($ menus) {$ id = $ level = 0; $ menuobjs = array (); $ tree = array (); $ notrootmenu = array (); foreach ($ menus as $ menu) {$ menuobj = new stdClass (); $ menuobj-> menu = $ menu; $ id = $ menu ['id']; $ level = $ menu ['Father _ id']; $ menuobj-> nodes = array (); $ menuobjs [$ id] = $ menuobj; if ($ level) {$ notrootmenu [] = $ menuobj;} else {$ tree [] = $ menuobj ;}} foreach ($ notrootmenu as $ menuobj) {$ menu = $ menuobj-> menu; $ id = $ menu ['id']; $ level = $ menu ['Father _ id']; $ menuobjs [$ level]-> nodes [] = $ menuobj;} return $ tree ;}}

My Weibo address: http://weibo.com/jameren,welcome to your attention ,.
By the way, make an advertisement:
Reference
I recently used a 15 GB ultra-high-space free network USB flash drive-@ cool disk, which can be used on PCs and mobile phones. it has no file type and size restrictions, fast transmission speed, and files will never be lost, let's try it together! Register through the invitation link below, and you and I will get an additional MB of cool disk space! Http://t.cn/aNLZje

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.