我将birtday日期存储在一个数组中
$birthdays = array
(
array("Alex",5,12),
array("Tom",2,20),
array("Sarah",6,12),
array("Anna",6,8)
array("Jonh",10,7) );
我需要html表格(2个输入文本框)我必须输入日期,按下按钮,如果日期存在,将打印今天过生日的人的姓名
答案 0 :(得分:2)
你可以试试这样的......
$SearchValue = "Sarah"
foreach ($birthdays as $value)
{
if (is_array($value))
{
if (in_array($SearchValue, $value)
{
//prints the inner array if the inner array contains your search value
print_r($value);
}
}
}
答案 1 :(得分:2)
试试这个:
<?php
$birthdays = array
(
array("Alex",5,12),
array("Tom",2,20),
array("Sarah",6,12),
array("Anna",6,8),
array("Jonh",10,7)
);
function getBirthdayNameByDate($birthdaysArray, $day, $month)
{
foreach($birthdaysArray as $array)
{
if($array[1] == $month && $array[2] == $day)
return $array[0];
}
return null;
}
$result = getBirthdayNameByDate($birthdays, 8, 6);
var_dump($result);
答案 2 :(得分:0)
使用此功能:
function retrieveBirthdays( $birthdays, $month, $day=0 )
{
return array_values
(
array_filter
(
$birthdays,
function( $row ) use( $month, $day )
{
if( $day ) return ( $row[1]==$month && $row[2]==$day );
else return ( $row[1]==$month );
}
)
);
}
print_r( retrieveBirthdays( $birthdays, 2, 20 ) );
将输出:
Array
(
[0] => Array
(
[0] => Tom
[1] => 2
[2] => 20
)
)
相反,要获得所有6月生日,您可以使用:
print_r( retrieveBirthdays( $birthdays, 6 ) );
您将获得:
Array
(
[0] => Array
(
[0] => Sarah
[1] => 6
[2] => 12
)
[1] => Array
(
[0] => Anna
[1] => 6
[2] => 8
)
)
答案 3 :(得分:-3)
$birthdays = array (
array("Alex",5,12),
array("Tom",2,20),
array("Sarah",6,12),
array("Anna",6,8),
array("Jonh",10,7),
array()
);
foreach($birthdays as $birthday) {
if($birthday) {
echo $birthday[0]."<br/>";
echo $birthday[1]."<br/>";
echo $birthday[2]."<br/>";
}
}
看看上面代码是否有帮助,它只是在foreach中有一个if条件来检查数组是不是emptu