首页 > 解决方案 > 如果它是数字数组中的第 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 中应该有一种更优雅的方式来做到这一点。

有什么建议么?谢谢。

标签: phpnumbers

解决方案


您可以使用这样的模数计算:

// 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;

推荐阅读