我正在构建一个小部件,我在其到期日期创建了横幅广告 WordPress的)
array(2) {
[7]=>array(2) {
["title"]=>string(3) "ads"
["number"]=>int(2)
[1]=>array(2) {
["address-image"]=>string(63) "http://localhost/wordpress/wp-content/uploads/2016/08/120-3.jpg"
["expire"]=>string(10) "2017/03/10"
}
[2]=>array(2) {
["address-image"]=>string(63) "http://localhost/wordpress/wp-content/uploads/2016/08/120-3.jpg"
["expire"]=>string(10) "2016/10/20"
}
}
["_multiwidget"]=>int(1)
}
并且想要应用如下:(删除键[2])
array(2) {
[7]=>array(2) {
["title"]=>string(3) "ads"
["number"]=>int(2)
[1]=>array(2) {
["address-image"]=>string(63) "http://localhost/wordpress/wp-content/uploads/2016/08/120-3.jpg"
["expire"]=>string(10) "2017/03/10"
}
}
["_multiwidget"]=>int(1)
}
为此,我在function.php中编写了以下代码:
function remove_expired_ads(){
$widgets = get_option('widget_ads_banner_sidebars');
$count = count($widgets);
for ($x=1; $x<=$count ;$x++ ){
foreach ( $widgets as $key => $w){
if (!is_array( $w )) continue ;
if(!empty($w[$x]['expire'])) {
$today_date = date('Ymd');
$expire_date = $w[$x]['expire'];
$expire_year = substr($expire_date, 0, 4);
$expire_month = substr($expire_date, 5, 2);
$expire_day = substr($expire_date, 8, 2);
$expire_time = $expire_year . $expire_month . $expire_day;
if (($today_date >= $expire_time)) {
//dd($widgets[$key][$x]);
unset($widgets[$key][$x]);
//If this method to write the entire arrays will be deleted unset($widgets[$key]);
}
}
}
}
update_option('widget_ads_banner_sidebars', $widgets , true);
}
add_action('widgets_init','remove_expired_ads');
但结果如下:
array(2) {
[7]=>array(2) {
["title"]=>string(3) "ads"
["number"]=>int(2)
[1]=>array(2) {
["address-image"]=>string(63) "http://localhost/wordpress/wp-content/uploads/2016/08/120-3.jpg"
["expire"]=>string(10) "2017/03/10"
}
[2]=>array(2) {
["address-image"]=>string(0) ""
["expire"]=>string(0) ""
}
}
["_multiwidget"]=>int(1)
}
更新 当我打印自己的代码结果
array(2) {
["address-image"]=>
string(63) "http://localhost/wordpress/wp-content/uploads/2016/08/120-3.jpg"
["expire"]=>
string(10) "2016/10/20"
}
如何访问密钥(密钥[2])
抱歉,我的英语不是很好
答案 0 :(得分:0)
Unset()
应该有效。我已经更新了检查到期日的方式。实施它,看看它是否有任何区别。
function remove_expired_ads(){
$widgets = get_option('widget_ads_banner_sidebars');
$count = count($widgets);
for ($x=1; $x<=$count ;$x++ ){
foreach ( $widgets as $key => $w){
if (!is_array( $w )) continue ;
if(!empty($w[$x]['expire'])) {
$expire_date = strtotime($w[$x]['expire']);
if ((time() >= $expire_time)) {
unset($widgets[$key][$x]);
}
}
}
}
update_option('widget_ads_banner_sidebars', $widgets , true);
}
add_action('widgets_init','remove_expired_ads');
答案 1 :(得分:0)
你可以使用这个循环:
$now = strtotime('now');
foreach ($widgets as $key => $widget) {
if (!is_array($widget)) {
continue;
}
$noOfAdds = 0;
$widget = array_filter($widget, function ($ad) use (&$noOfAdds, $now) {
if (!is_array($ad) || !isset($ad['expire'])) {
return true;
}
if ($now >= strtotime($ad['expire'])) {
return false;
}
$noOfAdds += 1;
return true;
});
if ($noOfAdds) {
$widgets[$key] = $widget;
$widgets[$key]['number'] = $noOfAdds;
continue;
}
unset($widgets[$key]);
}
我们使用foreach
来遍历小部件,使用array_filter
来过滤掉过期的广告。如果窗口小部件的所有广告都已过期,则窗口小部件将被取消设置。如果窗口小部件中有有效广告,则会更新该号码。我使用strtotime
来比较日期。
这是working demo。