我正在尝试将今天的日期与存储在数组中的日期相匹配。我快到了,但不完全。
如果找到匹配项,我想建立一个链接,如果没有,则不做任何事情。
问题在于比赛不是预期的。 ie:出于测试目的,如果我更改其中一个阵列以匹配今天的日期(11-08),它就不会提取正确的节日名称。
任何帮助非常感谢。我原本试图改编这篇文章: - PHP multidimensional array search by value
$festivals = array (
1 => array(
'festivalname' => 'FestivalOne',
'eventname' => 'Red',
'link' => 'red',
'date' => '01-05',
),
2 => array(
'festivalname' => 'FestivalTwo',
'eventname' => 'Yellow',
'link' => 'yellow',
'date' => '02-02',
),
3 => array(
'festivalname' => 'FestivalThree',
'eventname' => 'Blue',
'link' => 'blue',
'date' => '02-08',
),
4 => array(
'festivalname' => 'FestivalFour',
'eventname' => 'Green',
'link' => 'green',
'date' => '31-10',
)
);
$today = gmdate("j-m");
$key = array_search($today, array_column($festivals, 'date'));
$keys = array_keys(array_column($festivals, 'date'), $today);
if ( $today == $festivals[$key]['link'] ) {
echo '<a href="http://example.com/festivals/'.$festivals[$key]['link'].'" title="Festival : '.$festivals[$key]['festivalname'].'">'.$festivals[$key]['festivalname'].'</a>';
};
答案 0 :(得分:0)
试试这个:
<?php
$festivals = array (
1 => array(
'festivalname' => 'FestivalOne',
'eventname' => 'Red',
'link' => 'red',
'date' => '01-05',
),
2 => array(
'festivalname' => 'FestivalTwo',
'eventname' => 'Yellow',
'link' => 'yellow',
'date' => '02-02',
),
3 => array(
'festivalname' => 'FestivalThree',
'eventname' => 'Blue',
'link' => 'blue',
'date' => '02-08',
),
4 => array(
'festivalname' => 'FestivalFour',
'eventname' => 'Green',
'link' => 'green',
'date' => '31-10',
)
);
$today = gmdate("j-m");
foreach($festivals as $item) {
if ( $today == $item['date'] ) {
echo '<a href="http://example.com/festivals/'.$item['link'].'" title="Festival : '.$item['festivalname'].'">'.$item['festivalname'].'</a>';
}
}
?>