thinkphp template volist label nested loop output multidimensional array

Source: Internet
Author: User

The use of nested volist tags in thinkphp enables the output of multidimensional arrays. Volist nested using a normal two-dimensional array, you can use the Volist label directly loop output. For multidimensional arrays, you need to use the volist tag again to nest the loop output for the array members. Build a multidimensional 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 '; use Print_r () The function prints the array as follows: Array ([0] = = Array ([fruits] = = Array ([0] =&gt ; Array ([fruits1] + apple 1 [fruits2] + Orange Sub 1 [fruits3] + banana 1)) [Vegetables] = Cabbage 1) [1] = = Array ([fruits] = = Array ([1] = ARR                       Ay (     [Fruits1] + apple 2 [fruits2] = orange 2 [fruits3] + bananas 2 )) [vegetables] + cabbage 2) Assign the variable to the template in the corresponding module operation (e.g. Index/display) and output the template: $this->assign ("Food", $food); $this->display (); template tpl/default/index/display.html:<p> variety:</p> &LT;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, first the $food variables are looped out. Because the member fruits of the $food array is also an array, the $VO [' fruits '] variable is recycled (note that there is no $ symbol). The output is as follows: Food variety: Fruit 1: Apple 1 fruit 2: Orange 1 Fruit 3: Banana 1 Vegetable: Cabbage 1 fruit 1: Apple 2 fruit 2: Orange 2 Fruit 3: Banana 2 vegetable: Cabbage 2 Similarly, this method allows you to loop through the array of more dimensions.

  

thinkphp template volist label nested loop output multidimensional array

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.