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)