Acumularea elementelor array-urilor în buclele PHP
Să găsim suma elementelor array-ului cu ajutorul unei bucle:
php<?php
$arr = [1, 2, 3, 4, 5];
$res = 0;
foreach ($arr as $elem) {
$res += $elem;
}
echo $res;
?>
Задание 1
Se dă array-ul:
php<?php
$arr = [2, 5, 9, 3, 1, 4];
?>
Aflați suma elementelor acestui array.
Задание 2
Se dă array-ul:
php<?php
$arr = [2, 5, 9, 3, 1, 4];
?>
Aflați suma elementelor care sunt numere pare.
Задание 3
Se dă array-ul:
php<?php
$arr = [2, 5, 9, 3, 1, 4];
?>
Aflați suma pătratelor elementelor acestui array.
Задание 4
Se dă array-ul:
php<?php
$arr = [2, 5, 9, 3, 1, 4];
?>
Aflați produsul elementelor acestui array.