数组中的子键的PHP平均值

2022-05-18 00:00:00 arrays php associative-array
Array (
  [JohnDoe] => Array (
  [id] => 23
  [name] => JohnDoe
  [logintimes] => 30
)
 [JaneDoe] => Array (
  [id] => 26
  [name] => JaneDoe
  [logintimes] => 9
)
 [Smith] => Array (
  [id] => 35
  [name] => Smith
  [logintimes] => 9
 )
)

如何将登录时间彼此相加(然后求出它们的平均值?)


解决方案

循环和获取非常简单

$counter = 0;
$total = 0;
foreach($array as $row) {
    $counter++;
    $total += $row['logintimes'];
}
echo $total / $counter;

相关文章