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

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

number of elements in php array

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

Syntax and use of the count () function

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

count the number of elements in php array

An array of 13 random elements was originally created. Each element is a string of characters of different contents and lengths. Then two items were removed and one added. Logically 13 - 2 + 1 = 12, but the count () function thinks differently. The following is the output code for this result.

php counting the number of elements in an array

Since an array was added, the number of elements should have increased by the number of elements in 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 array elements 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 on the supply of fruit to the store, then the element may be pineapple and various varieties of apples or pears. Entering three different data structures into all three assortment positions is impractical for many reasons.

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

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

count the number of elements in php array

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

Objects and Arrays

Arrays are unquestionable quality and efficiency for data presentation. Using arrays as collections of objects is especially effective. The classic cycle of iterating over array elements or working in the style of the stack: 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 does not matter how to work with the collection of objects, but you can always assign each element of such a collection its own function for determining the quantity.

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

php counting the number of elements in an array

Imagine the assortment as an object, which includes:

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

You can get three levels of the hierarchy of objects, and then it will be possible to determine not only the number of elements in the array. PHP will allow you to calculate the quantity of goods, their value, determine the time of sale by the conditions of validity, etc. Using data in the form of objects allows you to give them the right quality, from which it is always easy to get the exact quantity.


All Articles