ThinkPHP template Volist label nested loop method for outputting multi-dimensional arrays _ php instance

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

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.