Home > PHP Framework > ThinkPHP > How to query the length of a one-dimensional array in thinkphp

How to query the length of a one-dimensional array in thinkphp

WBOY
Release: 2022-04-06 11:28:44
Original
2993 people have browsed it

Method: 1. Use "for($i=0;$i

How to query the length of a one-dimensional array in thinkphp

The operating environment of this article: Windows 10 system, ThinkPHP version 5, Dell G3 computer.

How to query the length of a one-dimensional array in thinkphp

for loop format

for($i=0;$i<count($arr);$i++)
{
echo $arr[$i];
}
Copy after login

Return the length of the array

$count = count($arr)
Copy after login

The front-end gets the array length

{$arr|count}
Copy after login

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.

How to query the length of a one-dimensional array in thinkphp

In the template file, use the count method to obtain the length of the array, written as "{$data|count}".

How to query the length of a one-dimensional array in thinkphp

#Open the test method in the index controller in the browser to view the results.

How to query the length of a one-dimensional array in thinkphp

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template