-1

How from this array I can make an average ?

$array=Array
(
    "Element_1" => 255,
    "Element_2" => 95,
    "Element_3" => 100
);

I should get 150.

F__M
  • 1,518
  • 1
  • 19
  • 34

3 Answers3

1

You can sum the values in the array and then divide them by the count of elements in the array.

$sum = array_sum($array); 
$count = count($array); 

echo $avg = $sum / $count; 
Blueline
  • 388
  • 1
  • 10
0

Try this

$average = array_sum($array) / count($array);
shubham715
  • 3,324
  • 1
  • 17
  • 27
0

Use below code :

  • array_sum() : Returns the sum of values in an array
  • count() : Count all elements in an array, or something in an object

Code:

<?php
$array=Array
(
    "Element_1" => 255,
    "Element_2" => 95,
    "Element_3" => 100
);
echo $average = array_sum($array) / count($array);

Output:

150

Demo: Click Here

RJParikh
  • 4,096
  • 1
  • 19
  • 36