: This article describes how to nest the Volist tag in the ThinkPHP template to output multi-dimensional arrays. if you are interested in the PHP Tutorial, refer to it. This article describes how to generate a multidimensional array using the Volist tag nested loop in the ThinkPHP template. 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.