<?php
$games = [
["Call of Duty", "Shooter", 59.95],
["Rocket League", "Sport", 19.95],
["Assassins Creed", "RP", 49.95]
];
?>
// i have an assignment where they want me to get the average string length from the titles and a average game price and echo it out like :
// echo "average price: €" . $averageprice;
// echo "average length of titles: " . $averagetitle
it needs to include:
- number_format
- for loop
i have tried numerous options but only got into coding for half a year now and havent really gotten into php. tried looking up different functions to use but couldnt get my head wrapped around it so i wonderd if the amazing coding community could heklp me out.
3
Answers
Average is computed as
$sum_of_values/$number_of_values
, so you can use for loop to loop throught all the items and sum them up as for example$sum_of_prices += $current_item[2]
and finally divide it by length of array (count($games)
).For length of string, just find out length of string by
strlen($current_item[0])
and add it to sum of lengths of titles like in prices. Finally divide it by length of array. You can use same loop for this.Not sure to have understand right your request,
If games informations are always in the same position you can try something like this:
Try something like this.
It includes a for loop en the number_format function: