How to Count All Elements or Values in an Array in PHP?

Jun 09, 2022 . Admin



Hello Friends,

This is a short guide on php count elements in array Code Example. you will learn How to count all array elements in PHP. step by step explain How to Count All Elements or Values in an Array in PHP. I’m going to show you about How to Count Occurrence of Value in Array in PHP.

This article will give you simple example of How to Count All Elements or Values in an Array in PHP.

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...

#PHP