Infinitus classification does not require Recursive Generation of multi-dimensional arrays (trees ).

Source: Internet
Author: User

Infinitus classification does not require Recursive Generation of multi-dimensional arrays (trees ).

A practical problem recently encountered in the project: the background product classification uses the infinitus method (1 ),

(Figure 1)

Now, the foreground traverses the third-level Navigation Based on the category (result 2 is shown ),

(Figure 2)

You need to process the queried data into multi-dimensional arrays, so that the foreground can use multiple foreach nested traversal.
After collecting information from multiple parties on the Internet and verifying the solution, record the solution as follows:
1. query all data from the database (the ThinkPHP framework for the project );

2. perform preliminary processing on the data so that the "key" of the Two-dimensional array is equal to the id of the three-dimensional array and the data is saved to the new array $ tmp;

1   $info = D('classify')->select();2     foreach($info as $key=>$val){3         $tmp[$val['id']] = $info[$key];4   }

3. Call the generateTree () method to convert the $ tmp array to a corresponding multi-dimensional array (as shown in figure 3 );

1/** 2 * @ param [array] $ items [array to be processed] 3 * @ return [array] [Multi-dimensional array] 4 */5 function generateTree ($ items) 6 {7 $ tree = array (); 8 foreach ($ items as $ item) {9 if (isset ($ items [$ item ['pid ']) {10 $ items [$ item ['pid '] ['son'] [] = & $ items [$ item ['id']; 11} else {12 $ tree [] = & $ items [$ item ['id']; 13} 14} 15 return $ tree; 16}

4. render the processed data to the foreground for traversal.

(Figure 3)

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.