Home  >  Article  >  Backend Development  >  How to know the total number of array in php

How to know the total number of array in php

PHPz
PHPzOriginal
2023-04-20 15:03:341024browse

As a strongly typed interpreted language, PHP provides programmers with a lot of conveniences in data processing. Array is a commonly used data type in PHP and is often used to store a series of related data. In actual programming, we often need to know how much data is contained in an array. So, this article will delve into how to get the total number of an array in PHP.

1. Use the count statement

PHP provides a very simple way to get the total number of an array, which is to use the count statement. The count statement is a very commonly used statement in PHP, which is used to count the number of elements in an array.

For example, we have the following array:

$array = array("apple", "banana", "orange", "pear");

Then we can use the count statement to get the number of its elements, as shown below:

$count = count($array);
echo $count;

The output result is:

4

Similarly, we can also use the count statement for a two-dimensional array to get the total number of elements inside it.

$array = array(
    array(1, 2, 3),
    array(4, 5, 6),
    array(7, 8, 9)
);
$count = 0;
foreach ($array as $value) {
    $count += count($value);
}
echo "总数为" . $count;

The output result is:

总数为9

2. Use the sizeof statement

In addition to the count statement, we can also use the sizeof statement in PHP to get the number of elements of the array. The only difference between sizeof and count is that sizeof is just an alias for count.

For example, we have the following array:

$array = array("apple", "banana", "orange", "pear");

We can use the sizeof statement to get the total number of elements, as shown below:

$count = sizeof($array);
echo $count;

The output result is:

4

Similarly, we can also use the sizeof statement to obtain the total number of internal elements in a two-dimensional array.

$array = array(
    array(1, 2, 3),
    array(4, 5, 6),
    array(7, 8, 9)
);
$count = 0;
foreach ($array as $value) {
    $count += sizeof($value);
}
echo "总数为" . $count;

The output result is:

总数为9

However, it is worth noting that although sizeof is an alias of count, in some contexts, such as in a for loop, the performance of sizeof may be superior. in count.

3. Use the number of variables to calculate the total number of elements

In PHP, in addition to using the count and sizeof statements to obtain the total number of elements of the array, you can also calculate it through the number of variables.

For example, we have the following array:

$array = array("apple", "banana", "orange", "pear");

We can use the following method to get the total number of elements:

$count = 0;
foreach ($array as $key => $value) {
    $count++;
}
echo $count;

The output result is:

4

Similarly, we can also obtain the total number of elements inside the two-dimensional array through the number of variables.

$array = array(
    array(1, 2, 3),
    array(4, 5, 6),
    array(7, 8, 9)
);
$count = 0;
foreach ($array as $key1 => $value1) {
    foreach ($value1 as $key2 => $value2) {
        $count++;
    }
}
echo "总数为" . $count;

The output result is:

总数为9

Conclusion

The above are the three methods of how to get the total number of array elements in PHP. In actual programming, we can choose the appropriate method to obtain the total number of array elements according to the specific situation. Whether it is count, sizeof or the number of variables, they are all very simple and feasible methods, and the calculation speed is also very fast.

The above is the detailed content of How to know the total number of array in php. 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