所以我在這個陣列程式碼中
$numers = array ( array(198,208,109,11,636), array(198,188,84,55,756), array(194,206,113,13,531), array(184,213,127,18,101), array(194,213,127,23,110), array(984,213,127,44,125), array(184,213,127,88,980), array(995,211,129,63,888) );
現在我想計算第二列(208, 188, 206, 213, 213, 213, 213, 211)的列平均值(從第一個198, 198, 194, 184, 194, 984, 184, 995)依此類推,直到倒數第五列。
我不知道該怎麼做,所以有人可以向我解釋如何計算每列的列平均值。
P粉1158400762023-09-11 00:15:13
<?php $numers = array( array(198,208,109,11,636), array(198,188,84,55,756), array(194,206,113,13,531), array(184,213,127,18,101), array(194,213,127,23,110), array(984,213,127,44,125), array(184,213,127,88,980), array(995,211,129,63,888) ); $num_cols = count($numers[0]); for ($i = 0; $i < $num_cols; $i++) { $column = array_column($numers, $i); $average = array_sum($column) / count($column); echo "Column $i average: $average<br>"; }
輸出:
Column 0 average: 391.375 Column 1 average: 208.125 Column 2 average: 117.875 Column 3 average: 39.375 Column 4 average: 515.875