Coding Villa

Get sum and count of array element using array_sum, count in PHP

Author : Tahreem Anwar

Share with Friends
Views : 335
Ratings : 0
Bookmarks : 0
Farourities : 0

Description


In this simple article you will learn about get sum and count of array element using array_sum, count in PHP.


Advertisements


 

 

array_sum function of php:

 

Array_ sum is used to get the sum of all the values of an array. The array_sum function takes only one parameter that is array. We sum values of that array.

 

Code:

 

1.    <?php

2.    $sum_of_array=array(0=>"10",1=>"20",2=>"30");

3.    echo "sum of array values=".array_sum($sum_of_array);

4.    echo "<br/>";

5.    ?>

 

Output:

 

sum of array values=60

 

Explanation:

 

On line 2 we have declared and initialize $sum_of_array. On line 3 we have passed that array to array_sum function and print it. As we can see in the output the result is 60 the sum of all values that we initialize in an array.

 

Code:

 

1.    <?php

2.    $sum_of_array=array(0=>10,1=>20,2=>30);

3.    echo "sum of array values=".array_sum($sum_of_array);

4.    ?>

 

Output:

 

sum of array values=60

 

Explanation:

 

On line 2 we have declared and initialize $sum_of_array. On line 3 we have passed that array to array_sum function and print it. As we see in the output the result is 60 the sum of all values that we initialize in an array.

 

Code:

 

1.    <?php

2.    $int_string = array(56,"Grades",65);

3.    $addition = array_sum( $int_string);

4.    print"addition is ". $addition ;

5.    echo "<br/>";

6.    ?>

 

Output:

 

addition is 121

 

 

Explanation:

 

On line 2 we have declared and initialize $int_string. On line 3 we have passed that array to array_sum function and print it. As we can see in the output the result is 121 the sum of all numeric values that we initialized in an array.

 

Count function of php:

 

The count () function counts the elements/values indexes of an array. It takes one parameter that is array. We count value of that array using Count($array).

 

Code:

 

1.    <?php

2.    $students = array("Tahreem", "Rizwan", "Khurram", "Jamil");

3.    $total_student= count($students);

4.    echo "Total number of students are= ".$total_student;

5.    echo "<br/>";

6.    ?>

 

Output:

 

Total number of students are= 4

 

Explanation:

 

In the above code on line 2 we have declared an array $students and initialize it. Then we have passed it to count function of php. That count total number of values of array. The result is saved in $total_student. Then we have displayed it.

 

Code:

 

1.    <?php

2.    $students = array(1, 2,3, 4,4,5);

3.    $total_student= count($students);

4.    echo "Total number of students are= ".$total_student;

5.    ?>

Output:

 

Total number of students are= 6

 

Explanation:

 

In the above code on line 2 we have declared an array $students and initialize it. Then we have passed it to count function of php. That count total number of values in an array. The result is saved in $total_student. Then we have displayed it.

 



Article Tags
Share with Friends

Comments




Leave a Reply

Name (required)  
Mail (will not be published) (required)   
 
Enter This Code
Captcha
 

Advertisements

Article Categories

.NET


Databases


Mobile Development


Operating Systems


Web Development


Coding Villa on Facebook