如何平均来自多个平面数组的数据列?
How to average columns of data from multiple, flat arrays?
假设我有 4 个数组,每个数组中的值数量相同:
$array1 = array(0, 7, 5, 0);
$array2 = array(2, 6, 10, 0);
$array3 = array(4, 8, 15, 10);
$array4 = array(6, 7, 20, 10);
我想计算每个索引的所有 4 个数组的平均值。所以我应该得到这样的东西:
array(3, 7, 12.5, 5);
这是一个简单的解决方案,但您可以进一步概括它并使其通用,但它现在可以使用。可以相应更新:
注意:假设数组的数量与您提到的相同
$result = [];
for ($i = 0; $i < count($array1); $i++) {
$result [] = ($array1[$i] + $array2[$i] + $array3[$i] + $array4[$i]) / count($array1);
}
dd($result);
全动态功能:
我承担了构建完全动态函数的任务,您可以在其中输入任意数量的数组。我还添加了一个 null
检查,如下例所示,以防您需要跳过数组中的值。我希望你喜欢这段代码,花了我一些时间来编写代码。
函数:
# The three dots (...) allow for ANY amount of
# arrays to be inputted into the function
#
# Theoretically you can put 100 different arrays if you wanted
# e.g. arrayAverage($a1, $a2, $a3, $a4, $a5, $a6, ...etc)
function arrayAverage(...$array){
# Add each of the values you want to average out into
# separate temporary arrays for easy manageability.
# For this instance, the first value of the 4 arrays
# will be added to its own array, then the second
# value of the 4 arrays will be added to its own array,
# and so on and so on...
foreach($array as $arr){
for($i = 0; $i < count($arr); $i++){
if($arr[$i] !== null) $temparr[$i][] = $arr[$i];
}
}
# Now we can get the average of each of those arrays we created
# and put them into the "$averages" array, to be returned at the end
for($j = 0; $j < count($temparr); $j++){
$averages[] = array_sum($temparr[$j]) / count($temparr[$j]);
}
# Returns the averages in said array
return $averages;
}
Usage/Example:
# Arrays do NOT need the same number of values/keys as shown in "$array2"
$array1 = array(0, 7, 5, 0);
$array2 = array(2, 6, 10, 0, 100);
$array3 = array(4, 8, 15, 10);
$array4 = array(6, 7, 20, 10);
# Example on how to skip values just in case the need arises
# (So our averages won't be affected by having an extra number)
$array5 = array(null, null, null, null, 300);
$averages = arrayAverage($array1, $array2, $array3, $array4, $array5);
var_export($averages);
输出:[3, 7, 12.5, 5, 200]
实时沙盒演示:
要更动态地使用,例如 6 个或更多数组,您可以使用此代码:
$all_arrays = [
array(0, 7, 5, 0),
array(2, 6, 10, 0),
array(4, 8, 15, 10),
array(6, 7, 20, 10),
array(1, 2, 3, 4),
array(5, 6, 7, 8),
// more arrays
];
$each_array_count = count($all_arrays[0]); // 4
$all_arrays_count = count($all_arrays); // 6
$output = [];
for ($i = 0; $i < $each_array_count; $i++) {
for ($j=0; $j < $all_arrays_count; $j++) {
$output[$i] += $all_arrays[$j][$i] / $all_arrays_count;
}
}
echo "<pre>";
var_dump($output);
输出:(Demo)
Warning: Undefined array key 0 in /in/E783F on line 20
Warning: Undefined array key 1 in /in/E783F on line 20
Warning: Undefined array key 2 in /in/E783F on line 20
Warning: Undefined array key 3 in /in/E783F on line 20
<pre>array(4) {
[0]=>
float(3)
[1]=>
float(6)
[2]=>
float(10)
[3]=>
float(5.333333333333333)
}
另一种方法:
$arrays = [$array1, $array2, $array3, $array4];
$result = [];
for ($i = 0; $i < count($array1); $i++) {
$result[] = array_sum(array_column($arrays, $i)) / count($arrays);
}
工作example。
对于圆滑的 functional-style 片段,使用 array_map()
来“转置”数据行。这意味着数据列将传递给自定义函数。从那里,执行平均数学。
代码:(Demo)
$array1 = [0, 7, 5, 0];
$array2 = [2, 6, 10, 0];
$array3 = [4, 8, 15, 10];
$array4 = [6, 7, 20, 10];
var_export(
array_map(
fn() => array_sum(func_get_args()) / func_num_args(),
$array1,
$array2,
$array3,
$array4
)
);
// fn(...$col) => array_sum($col) / count($col), would also work
输出:
array (
0 => 3,
1 => 7,
2 => 12.5,
3 => 5,
)
请注意,当数组的长度不完全相同时,此技术将用 null
(计为 0
)填充列中的空白:Demo.
如果您的输入数组是单个 multi-dimensional 数组,您可以使用展开运算符将其解压缩为 array_map()
.
var_export(array_map(fn() => array_sum(func_get_args()) / func_num_args(), ...$arrays));
为防止 null
值影响计算,请在计算前过滤它们:Demo.
var_export(
array_map(
fn(...$col) => array_sum($col) / count(array_filter($col, fn($v) => !is_null($v))),
...$arrays
)
);
// or: fn(...$col) => array_sum($col) / count(array_diff($col, ['']))
假设我有 4 个数组,每个数组中的值数量相同:
$array1 = array(0, 7, 5, 0);
$array2 = array(2, 6, 10, 0);
$array3 = array(4, 8, 15, 10);
$array4 = array(6, 7, 20, 10);
我想计算每个索引的所有 4 个数组的平均值。所以我应该得到这样的东西:
array(3, 7, 12.5, 5);
这是一个简单的解决方案,但您可以进一步概括它并使其通用,但它现在可以使用。可以相应更新:
注意:假设数组的数量与您提到的相同
$result = [];
for ($i = 0; $i < count($array1); $i++) {
$result [] = ($array1[$i] + $array2[$i] + $array3[$i] + $array4[$i]) / count($array1);
}
dd($result);
全动态功能:
我承担了构建完全动态函数的任务,您可以在其中输入任意数量的数组。我还添加了一个 null
检查,如下例所示,以防您需要跳过数组中的值。我希望你喜欢这段代码,花了我一些时间来编写代码。
函数:
# The three dots (...) allow for ANY amount of
# arrays to be inputted into the function
#
# Theoretically you can put 100 different arrays if you wanted
# e.g. arrayAverage($a1, $a2, $a3, $a4, $a5, $a6, ...etc)
function arrayAverage(...$array){
# Add each of the values you want to average out into
# separate temporary arrays for easy manageability.
# For this instance, the first value of the 4 arrays
# will be added to its own array, then the second
# value of the 4 arrays will be added to its own array,
# and so on and so on...
foreach($array as $arr){
for($i = 0; $i < count($arr); $i++){
if($arr[$i] !== null) $temparr[$i][] = $arr[$i];
}
}
# Now we can get the average of each of those arrays we created
# and put them into the "$averages" array, to be returned at the end
for($j = 0; $j < count($temparr); $j++){
$averages[] = array_sum($temparr[$j]) / count($temparr[$j]);
}
# Returns the averages in said array
return $averages;
}
Usage/Example:
# Arrays do NOT need the same number of values/keys as shown in "$array2"
$array1 = array(0, 7, 5, 0);
$array2 = array(2, 6, 10, 0, 100);
$array3 = array(4, 8, 15, 10);
$array4 = array(6, 7, 20, 10);
# Example on how to skip values just in case the need arises
# (So our averages won't be affected by having an extra number)
$array5 = array(null, null, null, null, 300);
$averages = arrayAverage($array1, $array2, $array3, $array4, $array5);
var_export($averages);
输出:[3, 7, 12.5, 5, 200]
实时沙盒演示:
要更动态地使用,例如 6 个或更多数组,您可以使用此代码:
$all_arrays = [
array(0, 7, 5, 0),
array(2, 6, 10, 0),
array(4, 8, 15, 10),
array(6, 7, 20, 10),
array(1, 2, 3, 4),
array(5, 6, 7, 8),
// more arrays
];
$each_array_count = count($all_arrays[0]); // 4
$all_arrays_count = count($all_arrays); // 6
$output = [];
for ($i = 0; $i < $each_array_count; $i++) {
for ($j=0; $j < $all_arrays_count; $j++) {
$output[$i] += $all_arrays[$j][$i] / $all_arrays_count;
}
}
echo "<pre>";
var_dump($output);
输出:(Demo)
Warning: Undefined array key 0 in /in/E783F on line 20
Warning: Undefined array key 1 in /in/E783F on line 20
Warning: Undefined array key 2 in /in/E783F on line 20
Warning: Undefined array key 3 in /in/E783F on line 20
<pre>array(4) {
[0]=>
float(3)
[1]=>
float(6)
[2]=>
float(10)
[3]=>
float(5.333333333333333)
}
另一种方法:
$arrays = [$array1, $array2, $array3, $array4];
$result = [];
for ($i = 0; $i < count($array1); $i++) {
$result[] = array_sum(array_column($arrays, $i)) / count($arrays);
}
工作example。
对于圆滑的 functional-style 片段,使用 array_map()
来“转置”数据行。这意味着数据列将传递给自定义函数。从那里,执行平均数学。
代码:(Demo)
$array1 = [0, 7, 5, 0];
$array2 = [2, 6, 10, 0];
$array3 = [4, 8, 15, 10];
$array4 = [6, 7, 20, 10];
var_export(
array_map(
fn() => array_sum(func_get_args()) / func_num_args(),
$array1,
$array2,
$array3,
$array4
)
);
// fn(...$col) => array_sum($col) / count($col), would also work
输出:
array (
0 => 3,
1 => 7,
2 => 12.5,
3 => 5,
)
请注意,当数组的长度不完全相同时,此技术将用 null
(计为 0
)填充列中的空白:Demo.
如果您的输入数组是单个 multi-dimensional 数组,您可以使用展开运算符将其解压缩为 array_map()
.
var_export(array_map(fn() => array_sum(func_get_args()) / func_num_args(), ...$arrays));
为防止 null
值影响计算,请在计算前过滤它们:Demo.
var_export(
array_map(
fn(...$col) => array_sum($col) / count(array_filter($col, fn($v) => !is_null($v))),
...$arrays
)
);
// or: fn(...$col) => array_sum($col) / count(array_diff($col, ['']))