Home >Backend Development >PHP Problem >How to get the length of an array in php

How to get the length of an array in php

PHPz
PHPzOriginal
2023-04-19 09:17:572063browse

Array is a very important data structure when programming in PHP. When we need to operate an array, we usually need to get the length of the array, that is, the number of elements it contains. This article will introduce several methods to obtain the length of a PHP array.

Method 1: count function

The count function is PHP’s own array length calculation function, which can return the number of elements of an array. The following is the syntax of this function:

count($array,COUNT_NORMAL);

Among them, the first parameter $array represents the array that needs to be calculated, and the second parameter COUNT_NORMAL represents using the default mode for calculation. In default mode, the count function recursively counts the number of elements in a multidimensional array.

You can easily get the length of an array using the count function. The following is a sample program:

<?php
    $arr = array("apple", "banana", "orange", "grape");
    $length = count($arr);
    echo "$length";
?>

Output result:

4

Method 2: sizeof function

The sizeof function has a similar function to the count function and can be used to calculate the length of the array . The following is the syntax of this function:

sizeof($array);

Among them, the parameter $array represents the array whose length needs to be calculated, which can be a one-dimensional or multi-dimensional array.

Use the sizeof function to get the array length. The following is a sample program:

<?php
    $arr = array("apple", "banana", "orange", "grape");
    $length = sizeof($arr);
    echo "$length";
?>

Output result:

4

Method 3: Array Iterator

In PHP5 and above, you can use the array iterator (ArrayIterator) to Iterate over an array and get its length. The following is a sample program using an array iterator:

<?php
    $arr = array("apple", "banana", "orange", "grape");
    $iterator = new ArrayIterator($arr);
    echo $iterator->count();
?>

Output result:

4

Method 4: Use a loop to calculate the length

Another way to get the length of an array is to use Loop through the array and count the number of elements. The following is a sample program:

<?php
    $arr = array("apple", "banana", "orange", "grape");
    $length = 0;
    foreach($arr as $value){
        $length++;
    }
    echo $length;
?>

Output result:

4

The above are several methods of obtaining the length of a PHP array. Readers can choose to use different methods according to actual needs. It should be noted that when the number of arrays is large, it may be time-consuming to use a loop to calculate the length. It is recommended to use the count or sizeof function.

The above is the detailed content of How to get the length of an 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