PHP count

王林
王林Original
2024-08-29 12:49:11531browse

The PHP count() method is used to get the number of elements in an array. The PHP count() method is a built-in method in PHP. It performs the same task as the size() method does. Sometimes we need to know the number of an element present in an array or object, so for this purpose PHP provides the count() method. It may also return 0 value for an array or object that has no element in it or if it is an empty array and also for an array or object which is not set.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax:

count( array, mode );

Parameters:

  • array: This is not an optional parameter, that specifies the array whose elements are to be count.
  • mode: This is an optional parameter, that specifies the mode of the method. There are two possible values for this parameter that are 0 and 1. The 0 indicates not to count the elements of an array recursively and this is the default value of this parameter. The 1 value indicates to count the elements of an array reclusively, which is used to count the elements of an array recursively for a multidimensional array.
  • return value: The return value of this method is the number of elements present in an object or array.

Working of PHP count() Method

  • The PHP count() method uses on the array or objects to get the number of elements present in the particular array. The array can be single dimensional or multidimensional.
  • When the count() method is called and pass the name of array to it, the count() method calculates how many elements are present in it and return this count as a result.

Examples of PHP count

Given below are the examples mentioned:

Example #1

Example of PHP count() method for finding the number of elements in an array. Next, we write the PHP code to understand the method more clearly with the following example, where the count() method is used to find the number of the element present in the single-dimensional array, as below.

Code:

<?php
// array of languages
$Language = array( 'English', 'Math', 'Science', 'Hindi');
print( "The array is : " );
print( implode(', ', $Language ));
print(".<br>");
// Calculating total number of elements present in an array.
$result = count($Language);
print( "The total number of elements present in an array are :" );
print( $result );
?>

Output:

PHP count

As in the above program the code “$result = count($Language);” find and return the number of elements is present in the array “Language” and which is displaying as well, as we can see in the output.

Example #2

Example of PHP count() method for finding the number of counts for the multidimensional array.

Next, we write the HTML code to understand the PHP Count() method, where the length property is used to finding the number of counts of an element in the multidimensional array, as below.

Code:

<?php
// multidimensional array of subjects
$sub = array( 'Subjects' => array( 'English', 'Math', 'Science', 'Hindi', 'Social' ),
'facultyid' => array( 'fac12', 'fac36', 'fac78', 'fac60', 'fac03' ) );
print( "The count for multidimensional array. " );
print( " <br>" );
// Calculating total number of elements present in an multidimensional array.
// recursive count
print( "The count for multidimensional array with recursive. " );
print( count( $sub, 1 ));
print( " <br>" );
print( "The count for multidimensional array with out recursive. " );
print( count( $sub ));
print( " <br>" );
?>

Output:

PHP count

As in the above program, the “sub” array is created to store the subjects and the respective faculty id who is handling it. Later in the code, the count() method is used two ways to find the number of elements present in this multidimensional array. The first way is “count( $sub, 1 );”, which is a recursive way as indicated by passing the mode value to 1 and the second way is “count( $sub );” , which is not a recursive way. So the output count of the first way is 12 and the output count of the second way is 2. PHP count() method for finding the number of counts for the multidimensional array of different data types.

Example #3

Next, we write the HTML code to understand the PHP Count() method, where the length property is used to find the number of counts of elements in the multidimensional array which is of different data types in different dimensions, as below.

Code:

<?php
// multidimensional array of Marks
$Marks = array( 'Subjects' => array( 'English', 'Math', 'Science', 'Hindi', 'Social' ),
'rollno' => array( 89, 78, 56, 45, 67 ));
print( "The count for multidimensional array of different data types. " );
print( " <br>" );
// Calculating total number of elements present in an multidimensional array.
// recursive count
print( "The count for multidimensional array with recursive. " );
print( count( $Marks, 1 ));
print( " <br>" );
print( "The count for multidimensional array with out recursive. " );
print( count( $Marks ));
print( " <br>" );
?>

Output:

PHP count

As in the above program, the “Marks” array is created to store the subjects and the respective marks which is of different data types compare to the subject of a marks array. Later in the code, the count() method uses two ways to find the number of elements present in this multidimensional array, so the output count of the first way is 12 and the output count of the second way is 2.

Conclusion

The PHP count() method is a built-in method, which is used to get the number of elements present in an array.

The above is the detailed content of PHP 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
Previous article:PHP ReferencesNext article:PHP References