thinkphp template volist label nested loop output multidimensional Array method _php instance

Source: Internet
Author: User
Tags php template smarty template
This article describes the thinkphp template volist label nested loop output multidimensional array method. Share to everyone for your reference, as follows:

The use of nested volist tags in thinkphp enables the output of multidimensional arrays.

Volist nested use

A general two-dimensional array that can be directly looped out with volist tags. For multidimensional arrays, you need to use the volist tag again to nest the loop output for the array members.

To 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 the Print_r () function to print out the array 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 the variable to the template in the corresponding module operation (such as Index/display) and output the template:

$this->assign ("Food", $food); $this->display ();

Template tpl/default/index/display.html:

Food varieties:


Fruit 1:{$f [' Fruits1 ']}

Fruit 2:{$f [' Fruits2 ']}

Fruit 3:{$f [' Fruits3 ']}

Vegetables: {$vo [' vegetables ']}


Inside the template, the $food variable is first 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 results are 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

In this way, the array of more dimensions can be looped out.

More interested in thinkphp related content readers can view this site topic: "thinkphp Introductory Tutorial", "thinkphp Common Methods Summary", "Smarty Template Primer Basic Tutorial" and "PHP template technology Summary."

It is hoped that this article is helpful to the PHP program design based on thinkphp framework.

  • 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.