How to get the length of PHP array

Source: Internet
Author: User
PHP array length of the method, PHP provides us with two functions to calculate the length of a one-dimensional array, I believe that the function of getting the length of the array is not very clear, today we will take you to learn more about the length of the PHP array to get the method ~

Getting the array length method in PHP is simple, PHP provides us with two functions to calculate the length of one-dimensional array, such as count,sizeof can be directly statistical array length Oh, let's look at a few examples.
How PHP Gets the length of the array, using the PHP function count (), or sizeof ()
For example:

The code is as follows:

$arr = Array (' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 '); echo count ($arr);//Output 5$arr = Array (' A ', ' B ', ' C '); Echo sizeof ($arr);//Output 3

sizeof () and count () have the same purpose, both of which can return the number of array elements. You can get the number of elements in a regular scalar variable, if the array passed to the function is an empty array, or is an array of variables that are not set, the number of elements returned is 0;
Like the function of two functions, the manual says that sizeof () is the alias of the function count ().

So how to count the length of multidimensional arrays? Continue to see examples
For example, the array you are reading is a two-dimensional array:

The code is as follows:

<?php$arr=array (                 0=>array (' title ' = ' News 1 ', ' viewnum ' = ' 123, ' content ' = ' ZAQXSWEDCRFV '),                 1 = >array (' title ' + ' News 2 ', ' viewnum ' = +, ' content ' = ' qwertyuiopzxcvbnm ')                ); >

If you want to count the length of the array $arr, which means that the two-dimensional array has only two news, you want the number is also 2, but if you use the Count ($arr) different versions of PHP, the results of the statistics are not the same;
Later found in the PHP manual, the Count function has a second parameter, which is explained as follows:
The Count function has two parameters:
0 (or Count_normal) is the default and does not detect multidimensional arrays (arrays in arrays);
1 (or count_recursive) for detecting multidimensional arrays,
So if you want to judge the reading of the array $arr is not a news message, it is necessary to write:

The code is as follows:

<?phpif (Is_array ($arr) && count ($arr, Count_normal) >0) {  ...}-else {  ...}? >

You can use this code to test the function:

The code is as follows:

<?php$arr=array (                 0=>array (' title ' = ' News 1 ', ' viewnum ' = ' 123, ' content ' = ' ZAQXSWEDCRFV '),                 1 = >array (' title ' = ' News 2 ', ' viewnum ' = ' content ' = ' qwertyuiopzxcvbnm ')               ; Echo ' does not count multidimensional arrays: '. COUNT ($ arr,0);//count ($arr, Count_normal) echo "<br/>"; Echo ' statistics multidimensional array: '. Count ($arr, 1);//count ($arr, count_recursive)? >

Summarize:

This article describes the acquisition of the length of the array of two functions, I believe you have a further understanding of this, you can choose the appropriate one according to their own needs, I hope that your work has helped!

Related recommendations:

Methods for judging php array dimensions (php array length)

Examples of PHP array length functions

PHP array, php array length _php tutorial

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.