PHP array length
PHP array length is defined as an array which is used to get many elements on them. Using the count () function and size of (), we could able to retrieve the count of an element. An array contains either string or integer values which can be either single or multi-dimensional. The array is used to hold a value in a key-value pair, an indexed array with many in-built functions for array processing. Using two Right functions (pre-defined) here saves a lot of time in calculating the values.
ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock TestsStart Your Free Software Development Course
Web development, programming languages, Software testing & others
Syntax
PHP array length is defined as follows:
Count(array name, mode);
This function takes two parameters, namely array name followed by a mode that denotes the dimensions. An empty array denotes zero and returns ‘1’ for non-array values.
How does array length Work in PHP?
Array length is determined by the count and size of the function. As per the syntax, the optional mode argument is set to count() recursively, which will recursively count the number of elements in an array. This function is intensively used in Multi-dimensional array.
To know an array length, we have a few common reasons:
- Using a ‘for’ loop to move through the elements.
- No of the search elements returned.
- Calculating average values in an array.
But in PHP, to get the number of elements in an array, either sizeof or count function is enabled here to predict the array length in PHP. As the number of elements changes concerning user requirements in our code, it is very important to see the actual length of the array list. PHP has two in-built functions, namely count and size of.
Using count (): To count the elements.
We can use like this:
Code:
$a1=array(6,3,1,9); echo " The size is given as =", count($a1);
So here, a count function returns the number of elements in an Object and simply counted in an associative array. In the above sample code, we have used a one-dimensional array. We have used PHPs native function count, so when we execute the above snippets, the output of the function is ‘4’. This is how we can get the values.
The second case is when we count the elements using parameter mode, to perform this, we have passed a constant ‘recursive’ as a parameter to count the elements. In this case, the length of an array is determined differently.
Code:
$avar = array (2,6,7, array (19,18,60)); $nelem = count ($avar, COUNT_RECURSIVE); echo $nelem;
The above code displays the output as ‘7’ instead of the value ‘6’.
To perform iteration in array elements, we can use for-loop for iteration. The values should loop continues to execute. So, in each iteration step, the value gets incremented by 1. Care should be taken when using for loop in count () method as PHP lacks in differentiating indexed array and associative array. But most programmer developers pretend to use count () instead of sizeof() as it returns the memory size. Even though it is similar to the count () function, most of them stick to the count() function.
Examples of PHP array length
Two methods define PHP array length or size count. Let’s see how these methods used to determine the length in the following examples.
Example #1
Creating a simple array to count the elements.
Code:
<?php $flowers= ['Jasmine', 'Diasy', 'Rose']; echo "The count is: " . count($flowers); ?>
Explanation:
- When we execute the above code snippets, the output is shown as ‘3’ as the array elements have 3 elements.
- First, we had created an array of ‘flowers’, and in the next line, we used the count command.
Output:
Example #2
Code:
<?php $program = [ 'C++' => ['Polymorphism', 'Inheritance', 'Template'], 'Java' => ['Interface', 'Multithread', 'Exception'], 'PHP' => ['ArrayLength', 'Count'] ]; echo "No. of count: ". count($program)."<br>"; echo "Multidimensional count: ". count ($program, 1); ?>
Explanation:
- Determines an array length with the count of ‘1’.
Output:
Example #3
Code:
<!DOCTYPE html> <html> <body> <?php $bike=array ( "Hero Splender"=>array ( "HP2345", "HS3456" ), "Royal Enfield"=>array ( "R3", "Tr5" ), "Honda Activa 6G"=>array ( "Classic 250" ) ); echo "General count: " . sizeof($bike)."<br>"; echo "Recursive Number: " . sizeof($bike,1); ?> </body> </html>
Explanation:
- The above code determines the General count as ‘3’ and the array length of recursive mode to be ‘8’.
Output:
Example #4
Using For-loop.
Code:
<?php $arr_iter = array (26,60,70,10,130,67); echo "No of elements in the array = ", sizeof($arr_iter), "<br /><br />"; //Iterating through the array for ($k=0; $k <sizeof($arr_iter); $k++){ echo "List of elements are: $arr_iter[$k] <br />"; } ?>
Explanation:
- The above code takes the length of an array using the sizeof function, and the array elements are iterated using for-loop.
- The output shows the list of values in an array in each iteration.
Output:
Example #5
Using Null value in mode.
Code:
<?php $m[0] = 2; $m[1] = 6; $m[2] = 8; value_res(count($m)); $n[3] = 1; $n[4] = 3; $n[8] = 5; value_res(count($n)); value_res(count(null)); value_res(count(false)); ?>
Explanation:
- The above code returns a parameter array as the value is assigned as null. So the output looks like this.
Output:
Example #6
Array length Using 2D array.
Code:
<?php $foods = array('choclates' => array('Diary Milk', 'Cadbury Godiva', 'Nestle','Snikkers', 'Candy Craze'), 'Fast Food' => array('Nuggets', 'Salad Platters')); echo count($foods, 1); echo "</br>"; echo sizeof($foods, 1); ?>
Explanation:
- In the above code, we have used mode count as ‘1’; therefore, this multi-dimensional array counts the value as ‘9’.
Output:
Example #7
Word Count.
Code:
<?Php $stringtype=' This is EDUCBA Asia largest Web Learning Platform providing courses in various Domains. We Provide Certification from many Leading Universities across the globe.'; $my1_array=explode(" ",$stringtype); echo "No.Of words in the String = ".sizeof($my1_array); ?>
Explanation:
- The above program Stores a paragraph in a variable of type String.
- Here an array is created using explode function to split the array.
- Finally, count the number of words in a paragraph.
- We get the count as below.
Output:
Conclusion
Here we have seen how to determine the length or size of an array in PHP and also the various methods to show how PHP functions are used to take memory size used by the array. There is no difference between the count and size of the function. Depends upon the developer, the methods are picked while writing the code. In this article, we explored PHP’s array length with many examples, and also, we have also seen more about multi-dimensional arrays.
The above is the detailed content of PHP array length. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.
