PHP array length or size count

count( $array, $mode)
ParameterDESCRIPTION
$arrayRequired : Input array to count number of elements present inside
$modeOptional : COUNT_RECURSIVE (or 1) , Useful for multidimensional array
We can add element to array or remove element from an array. To know the total length or total number of element present in an array we have to use count() command.

We will first create an array like this
$array = array  ("ABC","DEF", "GHI","KLM");
Now we will use the count command to know the number of elements in this array.
print count($array);

This will output 4 as there are four elements inside the array.


You can use sizeof function also to determine the number of values
$value= array(2,5,6,8,9);
echo "size of array = ".sizeof($value)."<br>"; // Output = 5
The above line output will be 5

With COUNT_RECURSIVE (or 1)

We can use optional parameter mode to recursively count the array. ( Total number of keys up to 2 levels )
<?Php
$a=array(array("product"=>"apple","quantity"=>2),
array("product"=>"Orange","quantity"=>4),
array("product"=>"Banana","quantity"=>5),
array("product"=>"Mango","quantity"=>7),
);
echo count($a); // Output = 4
echo "<br>";
echo count($a,1); // Output = 12
?>
Output is here
4
12
You can use sizeof() in above code
echo sizeof($a); // Output = 4
echo "<br>";
echo sizeof($a,1); // Ouput = 12

Words Count ( Sample projects using sizeof function)

we can use sizeof function to count number of words present in paragraph. Here are the steps involved.
1. Store the paragraph of text in a string variable 
2. Break the variable and create an array by using explode() function with space as delimiter
3. Count the number of elements present in the array.
Here is the sample code.
<?Php
///Storing the sample paragraph in a variable /////
$string='This is a sample string used to count the number of words present in a paragraph of data. You can read this sample text by asking user to enter them in a textara or you can directly read the data present in a file by using PHP file commands.';

///Creating the array by using split function ////

$our_array=explode(" ",$string);

///Generating the output ////
echo "Total Number of words = ".sizeof($our_array);
?>
The output is 49

explode(): Breaking string to generate array sum of elements size_of function is used to list elements of an array
Array REFERENCE
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    owaispathan

    14-12-2008

    i m confused about one thing ... plz help me out i am using an array for displaying the records in a loop. but i need to count an array printing the same value of records (means how many times the same value is being printed by the array)... is there any particular way to do so ?
    smo

    20-12-2008

    You can use array_count_values function for this. There is a link to this tutorial inside Further readings section.
    anant

    07-02-2009

    can i use this function i my tpl files <input type='text' value='sizeof($MLSColl)' id='t1'>
    smo

    07-02-2009

    If any PHP code is getting executed inside tpl file then this will also work.
    Sujoy

    09-02-2009

    count array size of array of array ??
    Muditha

    03-11-2009

    many thanx,i used this site,thanx very much,there r many many thing i didnt know,but i learnt a lot from here.
    Pankaj

    28-01-2013

    what id reason of count function add 0 after print length of array? like $a=Array(1,2,3,4,5); $b=count($a) b=50 ---- 0 is why ? tell me answer and another Question is how can i get only key of associative array not their value?
    smo

    31-01-2013

    If you try to print the value of $b it will be 5 only. Not 50. You can get only keys by using array_keys() function.
    Ram Kumar

    12-06-2014

    Array
    (
    [0] => Array
    (
    [Dis_id] => Dl-Dis1
    [Dis_Desc] => Discount
    [Dis_Per] => 7.500
    [Dis_val] => 67.50
    )

    )
    Ram Kumar

    12-06-2014

    Array
    (
    [0] => Array
    (
    [Dis_id] => Dl-Dis1
    [Dis_Desc] => Discount
    [Dis_Per] => 7.500
    [Dis_val] => 67.50
    )

    )
    i want the output 4 how ??

    Post your comments , suggestion , error , requirements etc here





    PHP video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer