i am storing birtday dates in an array
$birthdays = array
(
array("Alex",5,12),
array("Tom",2,20),
array("Sarah",6,12),
array("Anna",6,8)
array("Jonh",10,7) );
i need html form(2 input textbox)i have to enter the date,press the button and if the date is exist,will print the name of person who have a birthday today
You could try something like this...
$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);
}
}
}
Try this:
<?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);
With this function:
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 ) );
will output:
Array
(
[0] => Array
(
[0] => Tom
[1] => 2
[2] => 20
)
)
Instead, to obtain all june birthdays, you can use:
print_r( retrieveBirthdays( $birthdays, 6 ) );
and you will obtain:
Array
(
[0] => Array
(
[0] => Sarah
[1] => 6
[2] => 12
)
[1] => Array
(
[0] => Anna
[1] => 6
[2] => 8
)
)
$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/>";
}
}
See if above code helps, it simply has a if condtion in foreach to check that the array is not emptu
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.