top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How can i count the number of elements of an array in PHP?

+1 vote
540 views
How can i count the number of elements of an array in PHP?
posted Jun 3, 2014 by Rahul Mahajan

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

0 votes

The following functions can be used for calculating no. of elements in an Array.
1. sizeof($array)
2. count($array)

answer Jun 4, 2014 by Mohit Sharma
0 votes

Syntax
count($array, $mode );
Definition and Usage
Count elements in an array, or properties in an object.

If the optional mode parameter is set to COUNT_RECURSIVE (or 1), count() will recursively count the array. This is particularly useful for counting all the elements of a multidimensional array. The default value for mode is 0. count() does not detect infinite recursion.
Example
<?php $a[0] = 1; $a[1] = 3; $a[2] = 5; $result = count($a); print($result); ?>

This will produce following result:3

answer Jun 4, 2014 by Vrije Mani Upadhyay
Similar Questions
–1 vote

I want to display the number of views of my homepage and continuously increase the views whenever a user opens that page. How to achieve this using PHP and MYSQL?

0 votes

The output is in this form

Array (
     [0] => name
     [1] => email
     [2] => contact
     [3] => address 
    ) 
Array (
     [0] => sant
     [1] => sant@gmail.com
     [2] => **********
     [3] => haryana ) 

But I want the output in this form

  Array[1] (
     [0] => name
     [1] => email
     [2] => contact
     [3] => address 
    ) 
Array[2] (
     [0] => sant
     [1] => sant@gmail.com
     [2] => **********
     [3] => haryana
    ) 
...