如何删除PHP中的特定数组 [英] How do I delete a specific array in php
问题描述
我正在构建一个小部件,我创建了一个横幅广告,直到其有效期为止(在wordpress)
I am building a widget I created banner ads to its expiration date (in 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])
And would like to apply as follows: (Delete key [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中编写了以下代码:
For this I wrote the following code in the 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 When I print my own code result
array(2) {
["address-image"]=>
string(63) "http://localhost/wordpress/wp-content/uploads/2016/08/120-3.jpg"
["expire"]=>
string(10) "2016/10/20"
}
我如何访问密钥(密钥[2])
How do I access key (key [2])
对不起,我的英语不太好
Sorry, my English is not very good
推荐答案
您可以使用以下循环:
$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
来使日期具有可比性.
We use foreach
to traverse widgets and array_filter
to filter out expired ads. If all ads of the widget are expired the widget is unset. If there valid ads in the widget the number is updated. I have used strtotime
to make dates comparable.
这是工作演示.
这篇关于如何删除PHP中的特定数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!