PHP - count
Trieda
Metóda - count
(PHP 4, PHP 5, PHP 7)
Counts elements in an array or in an object.
To count objects, the Countable interface needs to be implemented.
Procedurálne
- function count (mixed $array_or_countable, int $mode = COUNT_NORMAL) : int
Parametre
| Názov | Dátový typ | Predvolená hodnota | Popis |
|---|---|---|---|
| $array_or_countable | mixed | An array or an object implementing the Countable interface. | |
| $mode | int | COUNT_NORMAL | We can set the COUNT_RECURSIVE constant as the second parameter. In that case, the function returns the number of elements even in all subarrays, so we can get the number of elements in multidimensional arrays. |
Mávratovej hodnoty
Vracia: int
The number of elements. If an array or a valid object with implemented Countable isn't set, it returns the value of 1. If it is NULL, it returns 0. Caution: the count() function returns 0 even for uninitialized variables.
Príklady
<?php
$array = [1, 2, 3, 4, 5, 6];
echo count($array);
Example with the COUNT_RECURSIVE parameter:
<?php
$array = [1, [4, 7, 11], 2, 3, 4];
echo count($array, COUNT_RECURSIVE);
