This article mainly introduces the method of generating multi-dimensional arrays by nested loop of Volist tags in the ThinkPHP template, and analyzes in detail the techniques related to the nested call of Volist tags to output multi-dimensional arrays using examples, for more information about how to use the Volist tag nested loop output of the ThinkPHP template, see the example in this article. We will share this with you for your reference. The details are as follows:
The nested volist label in ThinkPHP can be used to output multi-dimensional arrays.
Nested use of volist
A general two-dimensional array can be directly output cyclically using the volist tag. For multi-dimensional arrays, you need to use the volist label to nest the loop output for the array members.
Construct a multi-dimensional array:
$ Food = array (); $ food [0] ['fruits '] [0] ['fruits1'] = 'apple 1 '; $ food [0] ['fruits '] [0] ['fruits2'] = 'Orange 1 '; $ food [0] ['fruits '] [0] ['fruits3'] = 'banana 1'; $ food [0] ['Vegetables'] = 'cabbage 1 '; $ food [1] ['fruits '] [1] ['fruits1'] = 'apple 2 '; $ food [1] ['fruits '] [1] ['fruits2'] = 'Orange 2 '; $ food [1] ['fruits '] [1] ['fruits3'] = 'banana 2'; $ food [1] ['Vegetables'] = 'cabbage 2 ';
Print the Array Using the print_r () function as follows:
Array ([0] => Array ([fruits] => Array ([0] => Array ([fruits1] => Apple 1 [fruits2] => orange 1 [fruits3] => banana 1 )) [vegetables] => cabbage 1) [1] => Array ([fruits] => Array ([1] => Array ([fruits1] => Apple 2 [fruits2] => orange 2 [fruits3] => banana 2 )) [vegetables] => cabbage 2 ))
Assign variables to the template in the corresponding module operations (such as Index/display) and output the template:
$this->assign( "food", $food );$this->display();
Template Tpl/default/Index/display.html:
Food variety:
Fruit 1: {$ f ['fruits1']}
Fruit 2: {$ f ['fruits2']}
Fruit 3: {$ f ['fruits3']}
Vegetables: {$ vo ['Vegetables ']}
In the template, the $ food variable is output cyclically. Because the fruits of the $ food array is also an array, the $ vo ['fruits '] variable is output cyclically (note that there is no $ symbol ).
The output result is as follows:
Food type: fruit 1: Apple 1 fruit 2: Orange 1 fruit 3: banana 1 vegetables: cabbage 1 fruit 1: Apple 2 fruit 2: orange 2 fruit 3: banana 2 vegetables: cabbage 2
Similarly, this method can be used to output more multidimensional arrays cyclically.