php - 如果它是数字数组中的第 10 个数字,则在 PHP 中标记或标记一个数值
问题描述
我在 PHP 中有一个数组,其值如下
Array
([49] => 数组([配置文件] => 0.01 [值] => 220.3 [显示] => 0)
[48] => Array
(
[Profile] => 0.02
[Value] => 220.6
[Display] => 0
)
[47] => Array
(
[Profile] => 0.03
[Value] => 220.9
[Display] => 0
)
[46] => Array
(
[Profile] => 0.04
[Value] => 221.2
[Display] => 0
)
[45] => Array
(
[Profile] => 0.05
[Value] => 221.5
[Display] => 0
)
[44] => Array
(
[Profile] => 0.06
[Value] => 221.8
[Display] => 0
)
[43] => Array
(
[Profile] => 0.07
[Value] => 222.1
[Display] => 0
)
[42] => Array
(
[Profile] => 0.08
[Value] => 222.4
[Display] => 0
)
[41] => Array
(
[Profile] => 0.09
[Value] => 222.7
[Display] => 0
)
...
[41] => Array
(
[Profile] => 1.00
[Value] => 222.7
[Display] => 0
)
)
现在进入一个 foreach
foreach ($grid as &$gridData) {
$gridData['Display'] = is_grid_profile_10th_step ? 1 : 0;
}
我想在这个数组中从 10 到 10 个步骤来设置[Display] => 1
这样的值
0.1
0.2
0.3
...
0.9
1.0
被标记为[Display]
。
已经尝试过:
$gridData['Display'] = ($gridData['Profile'] * 10) % 10 == 0 ? 1 : 0;
- 这对我不起作用
$gridData['Display'] = is_int($gridData['Profile'] * 10) ? 1 : 0;
- 这也不起作用
但这确实:
$gridData['Display'] = intval($gridData['Profile'] * 10) == ($gridData['Profile'] * 10) ? 1 : 0;
我有一种感觉,在 PHP 中应该有一种更优雅的方式来做到这一点。
有什么建议么?谢谢。
解决方案
您可以使用这样的模数计算:
// round to 1 decimal
$rounded = round($gridData['Profile'], 1);
// Now check if the round changed the original value, if so, it was not
// a thenth number
$gridData['Display'] = $rounded == $gridData['Profile'] ? 1 : 0;
推荐阅读
- php - 管理员和用户 PHP 数据库登录
- parsing - AST类型可以在ocaml中递归吗?
- python - 检查特定值是否在 numpy 数组中多次出现
- python - 我想从文本文件中读取数据并将其保存到 Django 中的数据库中
- python - 从 macOS 中意外删除了 urllib3
- javascript - 如何将带有括号命名的属性的JSON字符串转换为Typescript
- sql-server - SSIS 变量变化时间分量
- javascript - 如何在 React 中将点击的组件中的数据从一个路由传递到另一个路由?
- vue.js - Vue.js 在输入事件上显示 div 会从输入中删除文本
- ruby-on-rails - 覆盖 ruby 类中的 module_function 并访问原始文件