We will get php array length using len()
and size()
method. This tutorial help to find total number of elements into an array. The PHP provides many method to find php length of array.
You can count number of elements into an array using PHP functions count()
and sizeof()
.
We will discuss these methods in this tutorial. The array can contain string and integer values. The array can be single dimensional or multidimensional.
Also checkout other related tutorials,
The count(array, mode) method takes two parameters, One is required(array name) and second(for multi dimensional array count element) is optional.The count()
will return 0 if the array is empty. And, if the variable specified is not an array, then, The count method will return 1.
Syntax:
count(var array, mode)
The first argument is an array variable that’s needed to calculate the PHP array length. The second argument is optional and it can take two values either COUNT_NORMAL
or COUNT_RECURSIVE
.
The count()
function will emit an E_WARNING error if a variable isn’t an array or a Countable object.
We can use count method as like below –
$num = array(1, 2, 3, array(4, 5, 6)); echo count($num)."\n"; echo count($num, 1);
Output:
4 7
The sizeof()
method is the alias of count function. This method function returns the number of elements in an array.
Syntax:sizeof(array, mode);
The array is the required element and which contains all elements. The mode parameter is optional – 0, Does not count all elements of multidimensional arrays.1, Counts the array recursively (counts all the elements of multidimensional arrays).
We can use sizeof()
method to get length of array in php, You can use sizeof method as like below –
$num = array(1, 2, 3, array(4, 5, 6)); echo sizeof($num)."\n"; echo sizeof($num, 1);
Output:
4 7
This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More
in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More
This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More
We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More
in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More
I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More