ThinkPHP template Volist label nested loop output multi-dimensional array method

Source: Internet
Author: User
: 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.

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.