php count array length elements with specific value


In PHP array length Example, array is an execution of an ordered Map. as well as A data map is an abstract data type of some key value term sets. this is one type of an interface, a arrangement.

List of all Google Adsense, VueJS, AngularJS, PHP, Laravel Examples.

Solution: Use the PHP count() as well as sizeof() length function

You can easy to use the get length of the total results find via PHP count() as well as sizeof() method to fetch the number of all the data elements read as well as find the values in an array. here available my prev Article The count() function and sizeof() function returns Boolean value like as 0 for a variable as a data that has been simply mode of the initialised with data value null or an empty array, so it might also Boolean value return 0 for a variable that are not well We can basically step by step to understand the below Flowchart.

array length in php-flowchart

count(array, mode)

PHP count() & sizeof() Function

You can additionally use the isset() function to check whether a variable is set or not.

<!DOCTYPE html>
<html lang="en">
    <title> Example of Counting an Array Values in PHP</title>

$players = array("Virat", "Rohit", "Krunal", "Sachin", "Dhoni", "Yuvraj", "Hardik");
// display of Printing players total array size
echo 'Total number of players in the $players array is - ' . count($players);
echo "<br>";
echo 'Total number of players in the $players array is - ' . sizeof($players);



Total number of players in the $players array is = 7
Total number of players in the $players array is = 7

How to create an Array in PHP?

An empty array


$emptyArray = array();


Single dimensional array


$players = array(“Virat”, “Sachin”, “Dhoni”);
$arrLength = count($players);

// loop through the array
for($i = 0; $i < $arrLength; $i++) {
    echo $players[$i];
    echo "</ br>";


Associative array


$players = array("Virat"=>"Batsmen", "Bhmrah"=>"Bolawer", "Sachin"=>"Batsmen");

// loop through get a associative all the array data value as well as retrive key value pairs
foreach($players as $key => $value) {
    echo "Key=" . $key . ", Value=" . $value;
    echo "</br>";


Two dimensional array


// two-dimensional array definition
$players = array

// two-dimensional array iteration
for ($row = 0; $row < 3; $row++) {
  echo "<p>Row number $row</p>";
  echo "<ul>";
  for ($col = 0; $col < 3; $col++) {
    echo "<li>".$players[$row][$col]."</li>";
  echo "</ul>";


Via PHP loop


$players = array();
for ($i = 0; $i < $count; $i++) {
    $players[$i] = array
        ( $playerName[$i]
        , $playerType[$i]


Example : PHP count() Function

Return the total number of data elements in an array like below example: array length in php

<!DOCTYPE html>

echo "<pre>";
echo count($players); 
echo "<br/><hr/>";
echo "<pre>";



    [0] => Virat
    [1] => Sachin
    [2] => Dhoni

Web Programming Tutorials Example with Demo

Read :


You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about array length in php.
I would like to have feedback on my blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Related FAQ

Here are some more FAQ related to this Article:

  1.   Form Submission With Validation Using Vue.JS and PHP
  2.   PHP Auto image resizing Script
  3.   How To Create Contact Form With PHPMailer To Send Mails
  4.   how to get last inserted id in laravel 6
  5.   install and/or enable a php accelerator (highly recommended)
  6.   CodeIgniter Enable Disable debug mode
  7.   cURL POST header parameters file json request
  8.   PHP Tutorial
  9.   PHP Array Length Size Count Tutorial With Example
  10.   How To Convert XML To Associative Array in PHP

Leave a Reply

Your email address will not be published. Required fields are marked *