需要你的帮助才能找到问题。程序将查找数组中的数据,如果匹配则显示名称,程序无法正常工作,仅在第一个数组中锁定数据。
<?php
$result = '';
$birthdays = array(
array("Alex",5,12),
array("Tom",2,20),
array("Sarah",6,12),
array("Anna",6,8),
array("Jonh",10,7)
);
if(isset($_POST)){
$d = isset($_POST['day_bd']) ? $_POST['day_bd'] : '';
$m = isset($_POST['month_bd']) ? $_POST['month_bd'] : '';
$result = getBirthdayNameByDate($birthdays, $d, $m);
$result = $result ? $result : 'no results found';
}
function getBirthdayNameByDate($birthdaysArray, $day, $month){
foreach($birthdaysArray as $array){
if($array[1] == $month && $array[2] == $day){
return $array[0];
}
return null;
}
}
?>
<!DOCTYPE HTML>
<html>
<head>
<style type="text/css">
form{
text-align: center;
}
input{
width: 50%;
margin-bottom: 20px;
line-height: 30px;
font-size: 25px;
}
.nameOut{
text-align: center;
margin-top: 40px;
border:4px solid darkred;
border-radius: 15px;
font-size: 2em;
text-transform: uppercase;
}
</style>
</head>
<body>
<form action =""method ="POST">
<label for="user_day">Day:
<input id="user_day" type="number" name="day_bd">
</label>
<br>
<label for="user_month">Month:
<input id="user_month" type="number" name="month_bd">
</label>
<br>
<input type="submit">
</form>
<?php if($result){ ?>
<div class="nameOut">
<?php
echo $result
?>
</div>
<?php } ?>
</body>
</html>
答案 0 :(得分:1)
向下移动return null;
一步。
function getBirthdayNameByDate($birthdaysArray, $day, $month){
foreach($birthdaysArray as $array){
if($array[1] == $month && $array[2] == $day){
return $array[0];
}
}
return null;
}
但请注意,您的计划只会返回第一场比赛。考虑将结果存储在一个数组中,因此不会遗漏生日ppl!