php - 在 array_sum 中计算时,如果数组中的值为空,则使用 $stoploss 作为 `$exit_price
问题描述
我正在尝试声明:如果数组中的计算 为空,则使用 $stoploss 作为array_sum
$stoploss
$exit_price
在下面的声明中:
$expectancy_a = array_sum(($exit_price - ($stoploss)))/$number_trades;
$stoploss
具有以下值:
array:10 [▼
0 => null
1 => null
2 => 425
3 => null
4 => 77
]
我怎么能在那个语句中说,如果 $stoploss 有空值,那么将 $exit_price 作为 $stoploss。
我厌倦了if
条件:
if (in_array(null, $stoploss, true)) {
$r_multiple[] = ($exit_price - $entry_price) / ($entry_price - $stoploss);
}
解决方案
您应该考虑使用使用空合并的闭包映射 $stoploss:
$corrected = array_map(function($value) use ($exit_price) {
return $value ?? $exit_price;
}, $stoploss);
这会将所有空值转换为 $stoploss 数组中的 $exit_price。
推荐阅读
- mediawiki - 为单独的项目/产品内容页面使用命名空间
- javascript - 可以用 ReactJS 中的 prop 值替换 API 调用的一部分吗?
- python - Pandas Excel 合并单元格
- django - AWS Lambda 函数中的 Django ORM
- github - 有没有办法在 Github 中搜索 PDF 资源中的特定文本
- amazon-web-services - 首次登录时的 AWS 访问/密钥
- reactjs - 从 switch 语句中渲染两个组件 react
- javascript - React Javascript,表单/输入验证不起作用
- python - 有没有办法让不和谐机器人以输入命令的用户身份发送消息?
- android - 2 日期选择器作为 android 中的微调器