2016-12-07 26 views
0

特定陣列我建立一個小窗口,我創建橫幅廣告,其到期日(以 WordPress的)如何刪除在PHP

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) 
} 

UPDATE 當我打印自己的代碼結果

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

您可以使用此循環:

$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遍歷widgets和array_filter過濾掉過期的廣告。如果該小部件的所有廣告都已過期,則該小部件未設置。如果窗口小部件中有有效的廣告,則會更新該數字。我用strtotime來比較日期。

這裏是working demo

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'); 
+0

謝謝,但不工作 – reza

+0

你仍然得到相同的結果嗎? – scottevans93

+0

我無權訪問密鑰 – reza