Home >PHP Framework >ThinkPHP >How to query the length of a one-dimensional array in thinkphp
Method: 1. Use "for($i=0;$i
The operating environment of this article: Windows 10 system, ThinkPHP version 5, Dell G3 computer.
for loop format
for($i=0;$i<count($arr);$i++) { echo $arr[$i]; }
Return the length of the array
$count = count($arr)
The front-end gets the array length
{$arr|count}
The example is as follows:
Create an IndexController.class in the ThinkPHP controller directory .php class file is used to explain how thinkphp finds the length of an array in a template file. Create a test() method to query data and pass it to the template.
Use the M method to instantiate the list model, query the data through the select() method, and save it in the $data variable. Pass the obtained data to the template using the assign() method, and display the template through the display() method.
In the template file, use the count method to obtain the length of the array, written as "{$data|count}".
#Open the test method in the index controller in the browser to view the results.
Summary:
1. In the index controller, create a test() method, use the M method to instantiate the list model, and pass select() ) method to query data.
2. In the test() method, pass the obtained data to the template using the assign() method, and display the template through the display() method.
3. In the template file, use the count method to obtain the length of the array, written as "{$data|count}".
Notes
If used in the if statement of a template, the writing method is "count($data)".
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to query the length of a one-dimensional array in thinkphp. For more information, please follow other related articles on the PHP Chinese website!