[英]How to remove NaN from array in PHP?
How do I remove NaN values from an array in php?如何从 php 中的数组中删除 NaN 值?
$array = [1, 3, 5, 3, float(NaN), 4, float(NaN)];
$desired_result = [1, 3, 5, 3, 4];
To remove any other element, I found this solution, but I can't get it to work for NaN.要删除任何其他元素,我找到了这个解决方案,但我无法让它为 NaN 工作。 https://stackoverflow.com/a/7225113/9606753
https://stackoverflow.com/a/7225113/9606753
Context: I am reading data from an rrd Database.上下文:我正在从 rrd 数据库中读取数据。 I want to calculate the mean across several data entries, however at least one of them is float(NaN).
我想计算多个数据条目的平均值,但其中至少一个是浮点数(NaN)。
Use array_filter to remove NaN with is_nan function.使用array_filter删除带有is_nan function 的 NaN。
$array = [1, 3, 5, 3, float(NaN), 4, float(NaN)];
$filtered_array = array_filter($array, function ($element) {
return !is_nan($element);
});
Note: This will also remove numbers that are stored as strings, '21' for example would be removed.注意:这也将删除存储为字符串的数字,例如“21”将被删除。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.