我的目的是检查一个数组,我将其中一个点设置为高分,如果在数组中任何其他3个得分与高分相同,则回显有2个相同的点其他回声不一样。所以基本上检查所有点是否有相同点。
我的代码
$Dpoint =70;
$Ipoint =70;
$Hpoint =60;
$Apoint =60;
$score = $Dpoint; high score
$xdata = array(
'D1' => $Dpoint,
'I1' => $Ipoint,
'H1' => $Hpoint,
'A1' => $Apoint
);
foreach($xdata as $key => $value){
if($score == $value){
echo "there is 2 same point";
}else{
echo "not the same";
}
}
但是我的结果很糟糕,我错过了哪一部分?解决我的问题
答案 0 :(得分:0)
如果我理解你的话......
<?php
$Dpoint =70;
$Ipoint =70;
$Hpoint =60;
$Apoint =60;
$score = $Dpoint;
$xdata = array(
'D1' => $Dpoint,
'I1' => $Ipoint,
'H1' => $Hpoint,
'A1' => $Apoint
);
$matches = 0;
foreach($xdata as $key => $value){
if($score == $value){
$matches ++;
}
}
echo $matches > 0 ? "$matches same point(s)": "no same points";
?>
答案 1 :(得分:0)
来自comments:
我可以跳过$ Dpoint自检吗?我只希望它回应“不一样” - babayaga 5分钟前
如果您不希望高分出现在unset()
,那么您可以使用array_search()
来获取密钥:
unset($xdata[array_search($score, $xdata)]);