PHP Count Elements in Array Code Example.

PHP

Nicesnippets

09-06-2022


PHP Count Elements in Array Code Example.

Hello Friends,

In this example, you will learn php count elements in array Code Example. I explained simply step by step How to count all array elements in PHP. I would like to share with you How to Count All Elements or Values in an Array in PHP. I explained simply step by step How to Count Occurrence of Value in Array in PHP. you will do the following things for how to get array count.

This article will give you simple example of PHP Count Elements in Array Code Example.

So, let's see bellow solution:

Example 1:


<?php

$ele = array("Jan","Feb","Mar","Apr","May","Jun");

$no_of_ele = count($ele);

echo "Number of element present in the array: ".$no_of_ele;

?>

Output:

Number of element present in the array: 6

Example 2:

<?php

$array = [

"C",

"DS",

"C++",

"80",

"70",

"60"

];

echo "Count second array elements: " . sizeof($array) . "<br>";

echo "<br>";

$array = [

'subject' => [

"C",

"DS",

"C++",

],

'marks' => [

"80",

"70",

"60"

]

];

echo ("Recursive count: " . sizeof($array, 1) . "<br>");

echo "<br>";

echo ("Normal count: " . sizeof($array, 0) . "<br>");

?>

Output:

Count second array elements: 6

Recursive count: 8

Normal count: 2

It will help you...