In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Today, the editor will share with you the relevant knowledge points about how to take the length of the php array, the content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.
There are two functions for php to get the length of the array: 1, count (), syntax "count ($arr,$m)"; 2, sizeof (), syntax "sizeof ($arr,$m)". The second parameter of these two functions is used to deal with multidimensional arrays and can be omitted; if the value is set to 1, the length of multidimensional arrays can be calculated.
Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
The method of getting the length of an array in PHP is simple, and PHP provides us with two functions to calculate the length of the array, namely the count () and sizeof () functions.
1. Count () function
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: the 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 0account 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.
Tip: if $array is neither an array nor an object, the count () function returns 1; if $array is equal to NULL, the count () function returns 0.
Example 1: one-dimensional array length
Example 2: length of a two-dimensional array
Looking at the output above, I wonder if there are only five elements in the array ("Zhang San", 25, "High Mathematics", "PHP tutorial", "English"), and why the length of the array is not 5, but 6.
In fact, at this time, the count () function loops statistics of all the elements in the two-dimensional array, "array (" High number "," PHP tutorial "," English ")" will be counted as a whole once, and its elements ("High number", "PHP tutorial", "English") will be counted again, so the final result is 6.
2. Sizeof () function
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.
Example: calculate the length of an array using the sizeof () function
These are all the contents of the article "how to get the length of the php array". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.