Home  >  Article  >  PHP Framework  >  ThinkPHP container you don’t know how to use count

ThinkPHP container you don’t know how to use count

咔咔
咔咔Original
2020-10-12 12:35:133084browse
"

This article mainly gives a brief introduction to count. Although it is used a lot, the small details have not been understood yet.

"

5. Count the stories you don’t know

Count the usages you don’t know

Now that we have said this, Kaka would like to popularize a usage of count that is not very common.

In the normal development process, this kind of usage is the most common and the most commonly seen use case.

As shown below, when an array is used directly, the length of the array is returned.

But if you are given a multi-dimensional array at this time, such as the picture below, and you are asked to count this multi-dimensional array, how should you count it?

At this time, it is estimated that the idea of ​​​​most friends is to loop and then define a counter to accumulate.

In fact, the count() function has already solved this requirement in this area.

The printed result below is "4----6"

Use the count() function directly to get an array and get the length of the first layer array.

But the count() function also has a second parameter. Setting it to 1 is to recursively count the number of elements in the array (count all elements in the multi-dimensional array)

So when you look at the documentation, you will find that the count() function itself has two parameters

The first parameter must be hungry, and the choice is an array

The second parameter The default value of the first parameter is 0, which means that all elements in the multi-dimensional array are not counted.

When the second parameter is 1, all elements in the multi-dimensional array are recursively calculated.

Carefully study every technical point, study a technical point thoroughly, and you will be able to do everything with ease no matter what you write or do.

Sometimes it is not necessary to remember everything, but you must know this thing so that you can take it out directly when you need it.

Persistence in learning, persistence in blogging, and persistence in sharing are the beliefs that Kaka has always upheld since his career. I hope that Kaka’s articles in the huge Internet can bring you a little Silk help. My name is Kaka, see you next time.

The above is the detailed content of ThinkPHP container you don’t know how to use count. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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