Php number of unique elements in the array.

Description

Return the number of elements of a variable var, which is usually an array , or any other object that can contain at least one element.

For objects count() returns the number of non-static properties, not taking visibility into account. If you have SPL support enabled, you can intercept count(), implementing the interface Countable. This interface has only one method, count(), which returns the value of the function count().

If var is not an array or an object that implements an interface Countable, will be returned 1 . With one exception: if var - NULL, it will be returned 0 .

Comment: Additional parameter mode was added since PHP 4.2.0.

If the additional parameter mode installed in COUNT_RECURSIVE(or 1), count() will count the number of array elements recursively. This is especially useful for counting all elements of multidimensional arrays. Preset parameter value mode - 0 . count() does not detect infinite recursion.

Warning

count() can return 0 for variables that are not set, but can also return 0 for variables that are initialized to an empty array. Use the isset() function to test whether a variable is set.

Please see the Array section of this tutorial for a detailed understanding of the implementation and use of arrays in PHP.

Example #1 Usage example count()

$a [ 0 ] = 1 ;
$a [ 1 ] = 3 ;
$a [ 2 ] = 5 ;
$result = count($a);
// $result == 3

$b [ 0 ] = 7 ;
$b[5] = 9;
$b [ 10 ] = 11 ;
$result = count($b);
// $result == 3;

$result = count(null);
// $result == 0;

$result = count(false);
// $result == 1;
?>

Description

Return the number of elements of a variable var, which is usually an array , or any other object that can contain at least one element.

For objects count() returns the number of non-static properties, not taking visibility into account. If you have SPL support enabled, you can intercept count(), implementing the interface Countable. This interface has only one method, count(), which returns the value of the function count().

If var is not an array or an object that implements an interface Countable, will be returned 1 . With one exception: if var - NULL, it will be returned 0 .

Comment: Additional parameter mode was added since PHP 4.2.0.

If the additional parameter mode installed in COUNT_RECURSIVE(or 1), count() will count the number of array elements recursively. This is especially useful for counting all elements of multidimensional arrays. Preset parameter value mode - 0 . count() does not detect infinite recursion.

Warning

count() can return 0 for variables that are not set, but can also return 0 for variables that are initialized to an empty array. Use the isset() function to test whether a variable is set.

Please see the Array section of this tutorial for a detailed understanding of the implementation and use of arrays in PHP.

Example #1 Usage example count()

$a [ 0 ] = 1 ;
$a [ 1 ] = 3 ;
$a [ 2 ] = 5 ;
$result = count($a);
// $result == 3

$b [ 0 ] = 7 ;
$b[5] = 9;
$b [ 10 ] = 11 ;
$result = count($b);
// $result == 3;

$result = count(null);
// $result == 0;

$result = count(false);
// $result == 1;
?>

The number of elements in a PHP array is proposed to be determined by the count() function. In most cases this is a simple and practical method. A regular website does not require special logic and does not contain complex systems of objects, so using the count() function will be sufficient.

In cases where arrays act as collections of objects or represent semantically related data structures, the calculation of quantity is determined by the meaning of the data.

Syntax and usage of the count() function

The result of the function is the number of elements contained in the array that are passed as a parameter. Below is a PHP example: counting the number of elements in an array.

Initially, an array of 13 random elements was created. Each element is a string of characters of varying content and length. Then two elements were removed and one was added. Logically, 13 - 2 + 1 = 12, but the count() function calculates differently. Below is the code to output this result.

Since an array was added, the number of elements should have increased by the number of elements of this array. It is clear that the purpose of the count() function is to count the number of elements in the array. PHP is least interested in the fact that the elements of an array can also be arrays. But in practical terms, array elements do not always include heterogeneous other arrays.

Features of data structuring

If we are talking about processing data for the supply of fruit to a store, then the element could be pineapple and various varieties of apples or pears. It is impractical to create three different data structures for all three assortment positions for many reasons.

One array will always be the best solution, but it can contain either a lowercase string - “pineapple”, or a collection of strings - “apple variety” or “pear variety”. There can be many lines, it all depends on:

  • delivery dates;
  • varieties;
  • quantities;
  • prices, etc.

But the meaning of the line of the entire array and the line of the subarray will always be the same. In this data representation, the number of elements in the PHP array cannot be determined by its logic. Here the array must have its own functionality for determining the quantity. The count() function is not recursive, and using it to determine the exact number of elements does not guarantee an accurate result.

Objects and Arrays

Arrays are an undeniable quality and efficiency for presenting data. It is especially effective to use arrays as collections of objects. Classic array elements or stack-style work: only with the first element or only with the last (after use, the element is deleted, but the next or previous one becomes available). It doesn't matter how you work with a collection of objects, but you can always assign each element of such a collection its own quantity determination function.

Then, without using the count() function, the number of elements in a PHP array can be determined by sequentially calling the method on each element in the array. Formally, this logic has a basis: there is an assortment of fruits supplied to the store, but what if the assortment increases, and plums are added to pineapples, apples and pears? What if, in addition to fruits, the store starts selling vegetables?

Let's imagine the assortment as an object that includes:

  • fruits;
  • vegetables;
  • the ability to add any product.

You can get three levels of object hierarchy, and then you can determine not only the number of elements in the array. PHP will allow you to calculate the quantity of a product, its cost, determine the time of sale based on expiration conditions, etc. Using data in the form of objects allows you to give them the desired quality, from which it is always easy to get the exact quantity.

Computers

How to determine the number of elements in a PHP array?

January 4, 2018

The number of elements in a PHP array is proposed to be determined by the count() function. In most cases this is a simple and practical method. A regular website does not require special logic and does not contain complex systems of objects, so using the count() function will be sufficient.

In cases where arrays act as collections of objects or represent semantically related data structures, the calculation of quantity is determined by the meaning of the data.

Syntax and usage of the count() function

The result of the function is the number of elements contained in the array that are passed as a parameter. Below is a PHP example: counting the number of elements in an array.

Initially, an array of 13 random elements was created. Each element is a string of characters of varying content and length. Then two elements were removed and one was added. Logically, 13 - 2 + 1 = 12, but the count() function calculates differently. Below is the code to output this result.

Since an array was added, the number of elements should have increased by the number of elements of this array. It is clear that the purpose of the count() function is to count the number of elements in the array. PHP is least interested in the fact that the elements of an array can also be arrays. But in practical terms, array elements do not always include heterogeneous other arrays.

Features of data structuring

If we are talking about processing data for the supply of fruit to a store, then the element could be pineapple and various varieties of apples or pears. It is impractical to create three different data structures for all three assortment positions for many reasons.

One array will always be the best solution, but it can contain either a lowercase string - “pineapple”, or a collection of strings - “apple variety” or “pear variety”. There can be many lines, it all depends on:

  • delivery dates;
  • varieties;
  • quantities;
  • prices, etc.

But the meaning of the line of the entire array and the line of the subarray will always be the same. In this data representation, the number of elements in the PHP array cannot be determined by its logic. Here the array must have its own functionality for determining the quantity. The count() function is not recursive, and using it to determine the exact number of elements does not guarantee an accurate result.

Objects and Arrays

Arrays are an undeniable quality and efficiency for presenting data. It is especially effective to use arrays as collections of objects. A classic loop of iterating through array elements or working in a stack style: only with the first element or only with the last (after use, the element is deleted, but the next or previous one becomes available). It doesn't matter how you work with a collection of objects, but you can always assign its own quantity determination function to each element of such a collection.

Then, without using the count() function, the number of elements in a PHP array can be determined by sequentially calling the method on each element in the array. Formally, this logic has a basis: there is an assortment of fruits supplied to the store, but what if the assortment increases, and plums are added to pineapples, apples and pears? What if, in addition to fruits, the store starts selling vegetables?

Let's imagine the assortment as an object that includes:

  • fruits;
  • vegetables;
  • the ability to add any product.

You can get three levels of object hierarchy, and then you can determine not only the number of elements in the array. PHP will allow you to calculate the quantity of a product, its cost, determine the time of sale based on expiration conditions, etc. Using data in the form of objects allows you to give them the desired quality, from which it is always easy to get the exact quantity.

Source: fb.ru

Current

Miscellaneous
Miscellaneous

You may have already encountered the task of counting the total number of lines in a file. It doesn’t matter if you need to count the number of service records in a file, find out the number of htaccess lines or records of a txt file that stores any data. The solutions below are suitable for each case.

Let's create a function to count the number of lines in a PHP file

The created function will use two built-ins:

1. file() takes a file or path to it as an argument and returns an array of strings
2. count() counts the number of elements of an array.

Now let's move on to creating a file with the function itself. In the root directory (you can choose another one at your discretion) create a separate folder include. In it we will create a file for the future function and call it functions.php. Next, insert the code below into it.

PHP code(file functions.php)

Function lines($file)
{
// first we look for the file itself. Maybe the path to it was specified incorrectly
if(!file_exists($file))exit("File not found");

// consider the file as an array
$file_arr = file($file);

// count the number of lines in the array
$lines = count($file_arr);

// output the result of the function
return $lines;
}

Echo lines("index.php"); // display a number - the number of lines in the index.php file

?>
So, the file with the function is ready. In the root directory (folder) we can already create a working file with any name (in the example it was called example.php), and in it we will connect this function as follows.

PHP code(file example.php)

// here we indicate the path to the file with the function
include_once "include/functions.php"; // or "functions.php" if the function is in the same folder as the working exemaple.php file

// store the number of lines (number) in the $count_lines variable
$count_lines = lines("index.php");

// display the result of the row counting function
echo "Lines in file: ".$count_lines;

?>
The result will display something like the following text

Lines in file: 52
You can look at the example more briefly without creating a function. There will no longer be any check for the presence of the file

PHP code(paste into any php file)

$file = "file.txt"; // specify the file itself and the path to it
$lines = count(file($file)); // calculate the number of lines
echo "The file $file has the number of lines $lines"; // display the result
?>
Example result

In the file file.txt the number of lines is 20
Thank you for your attention!