By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy

Q: Train - PHP Array Task

+3 votes

You will be given a count of wagons in a train n. On the next n lines you will receive how many people are going to get on that wagon. At the end print the whole train and after that the sum of the people in the train.


train php array task

asked in PHP category by user ak47seo

1 Answer

+2 votes

My answer:


$wagons = intval(readline());
$train = [];//empty array

$sum = 0;
for ($i = 0; $i < $wagons; $i++) {
    $current = intval(readline());
    $train[$i] = $current;
    $sum += $current;

echo implode(' ', $train) . PHP_EOL;
echo $sum;
answered by user andrew