如何比较数组php中的值
我在数组中有数据
[list] => Array
(
[0] => Array
(
[id] => 216
[name] => item A
[nilai] => 0.456
)
[1] => Array
(
[id] => 217
[name] => item B
[nilai] => 0.999
)
)
,如果值是最大的,我想制定条件,那么文本是绿色的 如何在foreach中使状况?
这是我的代码
<?php foreach($res['method']['list'] as $key=>$row) { ?>
<div class="form-check">
<input class="form-check-input" type="radio" name="flexRadioDefault" id="flexRadioDefault1">
<label class="form-check-label" for="flexRadioDefault1"><?php echo $row['nilai'] ?></label>
</div>
<?php } ?>
i have data in array
[list] => Array
(
[0] => Array
(
[id] => 216
[name] => item A
[nilai] => 0.456
)
[1] => Array
(
[id] => 217
[name] => item B
[nilai] => 0.999
)
)
here I want to make a condition if the value is the largest then the text is green
how to make the condition in foreach ?
this my code
<?php foreach($res['method']['list'] as $key=>$row) { ?>
<div class="form-check">
<input class="form-check-input" type="radio" name="flexRadioDefault" id="flexRadioDefault1">
<label class="form-check-label" for="flexRadioDefault1"><?php echo $row['nilai'] ?></label>
</div>
<?php } ?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在上面的代码中,我们首先将“ nilai”分开提取,并在foreach循环中使用该索引找到最大值并存储它的索引,我们可以实现所需的结果
In the above code we at first extract the 'nilai' in separate and find max value and store it's index using that index in foreach loop we can achieve the desired result
这将首先计算列表中的最大“ nilai”值,然后在array_map中为列表中的每个项目提供一个附加的键'max'的副本。该值将设置为“ true”或“ false”,具体取决于Nihai等于$ MAXNILAI。然后,您可以使用该“最大”布尔值以绿色或不使用绿色。
输出:
This will first calculate the maximum 'nilai' value in your list, and then in array_map a copy of your list will be created with an additional key 'max' for each item in your list. This value will be set to 'true' or 'false' depending on nihai being equal to $maxNilai or not. You then can use that 'max' boolean value to color in green or not.
Output: