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

0

3 Answers 3

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; 
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

0

Try this

$average = array_sum($array) / count($array);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.