Sunday 4 April 2021

Php: array: count: count number of elements in the array

count ( Countable|array $value , int $mode = COUNT_NORMAL ) : int

 

Description

Counts all elements in an array, or something in an object.

 

Parameters:

mode: If the optional mode parameter is set to COUNT_RECURSIVE (or 1), count() will recursively count the array. This is particularly useful for counting all the elements of a multidimensional array

 

array_count_elements.php

#!/usr/bin/php

<?php

    $arr1 = array(2, 3, 5, 7);
    $arr2 = [[1, 2, 3, 4], [5, 6, 7, 8]];

    $number_of_elements = count($arr1);
    echo "number of elements in the array arr1 : $number_of_elements\n";

    $number_of_elements = count($arr2, 1);
    echo "number of elements in the array arr2 : $number_of_elements\n";
?>

 

Output

$./array_count_elements.php 

number of elements in the array arr1 : 4
number of elements in the array arr2 : 10

 

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment