Get the App
SLTechnology News&Howtos  ›  Development  › 

How php gets several elements in an array

Shulou Source: shulou.com Published: 2022-06-02 10:00:32 09月20日 Update

This article introduces the knowledge of "how to get several elements in the array by php". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Php to get several elements in the array method: 1, the use of count () function, you can count the number of all elements in the array, the syntax "count (array,mode)"; 2, the use of sizeof () function, you can calculate the number of elements in the array, syntax "sizeof (array,mode)".

Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

It's easy to count the elements of an array in PHP. PHP provides us with two functions, the count () and sizeof () functions.

In fact, the sizeof () function is an alias for the count () function, that is, the function and usage of the sizeof () function is exactly the same as the count () function.

Let's introduce the count () function to understand these two functions.

The count () function can count the number of all elements in an array, or the number of attributes in an object. The syntax format is as follows:

Count (array,mode)

The parameters are described as follows:

Array: an array or object to be counted

Mode: optional parameter, which can be omitted.

If you omit the mode parameter, or set it to COUNT_NORMAL or the zero count () function will not detect multidimensional arrays

If mode is set to COUNT_RECURSIVE or the 1 count () function will recursively count the number of elements in the array, it is especially useful for counting the number of elements in a multi-dimensional array.

Example 1: count the number of elements in an one-dimensional array

Example 2: count the number of elements in a two-dimensional array

That's all for "how to get a few elements in an array by php". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Array function element statistics number number parameter syntax two content object quantity method more knowledge example multidimensional practical same learned Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Apple NVidia Shulou Tech Info Linux