php - Storing values to array and Counting total array values -
im storing age values inside array using array push,
foreach($arrayagevalues $value){ $arrayage21to30 = array(); $arrayage31to40 = array(); if($value['age'] >= 21 && $value['age'] <= 30) { array_push($arrayage21to30, $value['age']); } if($value['age'] >= 31 && $value['age'] <= 40) { array_push($arrayage31to40, $value['age']); } }
at end want count total values stored inside array in $arrayage21to30 ans $arrayage31to40. used count($arrayage21to30); doesnt return total number of array values inside array.
is there way on how can count values of array or approach of storing values inside array wrong?
$arrayage21to30
, $arrayage31to40
should out of foreach
just try code:
<?php $arrayage21to30 = array(); $arrayage31to40 = array(); foreach($arrayagevalues $value) { if($value['age'] >= 21 && $value['age'] <= 30) { array_push($arrayage21to30, $value['age']); } if($value['age'] >= 31 && $value['age'] <= 40) { array_push($arrayage31to40, $value['age']); } } echo count($arrayage21to30), "\n"; echo count($arrayage31to40), "\n";
if want calculate sum, can use arrray_sum
:
echo array_sum($arrayage21to30), "\n"; echo array_sum($arrayage31to40), "\n";
Comments
Post a Comment