ThinkPHP template Volist label nested loop output multi-dimensional array method, thinkphpvolist
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:
<P> food varieties: </p> <br/> <volist name = "food" id = "vo"> <volist name = "vo ['fruits ']" id = "f"> <p> fruit 1: {$ f ['fruits1']} </p> <p> fruit 2: {$ f ['fruits2']} </p> <p> fruit 3: {$ f ['fruits3']} </p> </volist> <p> vegetables: {$ vo ['Vegetables ']} </p> <br/> </volist>
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.