特定陣列我建立一個小窗口,我創建橫幅廣告,其到期日(以 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])
對不起,我的英語水平是不是很好
謝謝,但不工作 – reza
你仍然得到相同的結果嗎? – scottevans93
我無權訪問密鑰 – reza