Php 如何过滤带有日期的数组?
我需要删除“Vencimiento”与当前月份不同的任何位置 我有这个数组,它可以是可变的,所以我想如果过期的前6个字符(“2017-11”-xx)等于当前月份,我必须是foreach和search,并用日期()验证它 数组:Php 如何过滤带有日期的数组?,php,arrays,filter,Php,Arrays,Filter,我需要删除“Vencimiento”与当前月份不同的任何位置 我有这个数组,它可以是可变的,所以我想如果过期的前6个字符(“2017-11”-xx)等于当前月份,我必须是foreach和search,并用日期()验证它 数组: array(2) { [264]=> array(10) { [0]=> array(6) { ["idImpuesto"]=> int(10) ["idConcepto"]=>
array(2) {
[264]=>
array(10) {
[0]=>
array(6) {
["idImpuesto"]=>
int(10)
["idConcepto"]=>
int(19)
["periodo"]=>
string(4) "2017"
["tipoOperacion"]=>
string(12) "PRESENTACION"
["vencimiento"]=>
string(10) "2017-11-13"
["formularios"]=>
string(3) "713"
}
[1]=> //DELETE
array(6) {
["idImpuesto"]=>
int(10)
["idConcepto"]=>
int(19)
["periodo"]=>
string(4) "2017"
["tipoOperacion"]=>
string(12) "PRESENTACION"
["vencimiento"]=>
string(10) "2017-12-29" //IT'S NOT THE CURRENT MONTH
["formularios"]=>
string(3) "713"
}
[2]=> //DELETE
array(5) {
["idImpuesto"]=>
int(10)
["idConcepto"]=>
int(859)
["periodo"]=>
string(4) "2017"
["tipoOperacion"]=>
string(12) "PRESENTACION"
["vencimiento"]=>
string(10) "2017-12-13" //IT'S NOT THE CURRENT MONTH
}
[3]=>
array(5) {
["idImpuesto"]=>
int(10)
["idConcepto"]=>
int(19)
["periodo"]=>
string(4) "2017"
["tipoOperacion"]=>
string(4) "PAGO"
["vencimiento"]=>
string(10) "2017-11-13"
}
[4]=> //DELETE
array(6) {
["idImpuesto"]=>
int(10)
["idConcepto"]=>
int(191)
["periodo"]=>
string(4) "2018"
["anticipoCuota"]=>
int(1)
["tipoOperacion"]=>
string(4) "PAGO"
["vencimiento"]=>
string(10) "2017-12-13" //IT'S NOT THE CURRENT MONTH
}
[5]=> //DELETE
array(6) {
["idImpuesto"]=>
int(10)
["idConcepto"]=>
int(191)
["periodo"]=>
string(4) "2018"
["anticipoCuota"]=>
int(2)
["tipoOperacion"]=>
string(4) "PAGO"
["vencimiento"]=>
string(10) "2018-01-15" //IT'S NOT THE CURRENT MONTH
}
[6]=> //DELETE
array(5) {
["idImpuesto"]=>
int(10)
["idConcepto"]=>
int(859)
["periodo"]=>
string(4) "2017"
["tipoOperacion"]=>
string(4) "PAGO"
["vencimiento"]=>
string(10) "2017-12-13" //IT'S NOT THE CURRENT MONTH
}
[7]=> //DELETE
array(6) {
["idImpuesto"]=>
int(30)
["idConcepto"]=>
int(19)
["periodo"]=>
string(7) "2017-12"
["tipoOperacion"]=>
string(12) "PRESENTACION"
["vencimiento"]=>
string(10) "2018-01-18" //IT'S NOT THE CURRENT MONTH
["formularios"]=>
string(8) "731,2002"
}
[8]=> //DELETE
array(6) {
["idImpuesto"]=>
int(30)
["idConcepto"]=>
int(19)
["periodo"]=>
string(7) "2017-11"
["tipoOperacion"]=>
string(12) "PRESENTACION"
["vencimiento"]=>
string(10) "2017-12-18" //IT'S NOT THE CURRENT MONTH
["formularios"]=>
string(8) "731,2002"
}
[9]=>
array(6) {
["idImpuesto"]=>
int(30)
["idConcepto"]=>
int(19)
["periodo"]=>
string(7) "2017-10"
["tipoOperacion"]=>
string(12) "PRESENTACION"
["vencimiento"]=>
string(10) "2017-11-21"
["formularios"]=>
string(8) "731,2002"
}
}
[265]=>
array(9) {
[0]=> //DELETE
array(6) {
["idImpuesto"]=>
int(11)
["idConcepto"]=>
int(191)
["periodo"]=>
string(4) "2017"
["anticipoCuota"]=>
int(4)
["tipoOperacion"]=>
string(4) "PAGO"
["vencimiento"]=>
string(10) "2017-12-13" //IT'S NOT THE CURRENT MONTH
}
[1]=> //DELETE
array(6) {
["idImpuesto"]=>
int(30)
["idConcepto"]=>
int(19)
["periodo"]=>
string(7) "2017-12"
["tipoOperacion"]=>
string(12) "PRESENTACION"
["vencimiento"]=>
string(10) "2018-01-18" //IT'S NOT THE CURRENT MONTH
["formularios"]=>
string(8) "731,2002"
}
[2]=> //DELETE
array(6) {
["idImpuesto"]=>
int(30)
["idConcepto"]=>
int(19)
["periodo"]=>
string(7) "2017-11"
["tipoOperacion"]=>
string(12) "PRESENTACION"
["vencimiento"]=>
string(10) "2017-12-19" //IT'S NOT THE CURRENT MONTH
["formularios"]=>
string(8) "731,2002"
}
[3]=>
array(6) {
["idImpuesto"]=>
int(30)
["idConcepto"]=>
int(19)
["periodo"]=>
string(7) "2017-10"
["tipoOperacion"]=>
string(12) "PRESENTACION"
["vencimiento"]=>
string(10) "2017-11-22"
["formularios"]=>
string(8) "731,2002"
}
[4]=> //DELETE
array(5) {
["idImpuesto"]=>
int(30)
["idConcepto"]=>
int(19)
["periodo"]=>
string(7) "2017-12"
["tipoOperacion"]=>
string(4) "PAGO"
["vencimiento"]=>
string(10) "2018-01-18" //IT'S NOT THE CURRENT MONTH
}
[5]=> //DELETE
array(5) {
["idImpuesto"]=>
int(30)
["idConcepto"]=>
int(19)
["periodo"]=>
string(7) "2017-11"
["tipoOperacion"]=>
string(4) "PAGO"
["vencimiento"]=>
string(10) "2017-12-19" //IT'S NOT THE CURRENT MONTH
}
[6]=>
array(5) {
["idImpuesto"]=>
int(30)
["idConcepto"]=>
int(19)
["periodo"]=>
string(7) "2017-10"
["tipoOperacion"]=>
string(4) "PAGO"
["vencimiento"]=>
string(10) "2017-11-22"
}
[7]=> //DELETE
array(5) {
["idImpuesto"]=>
int(308)
["idConcepto"]=>
int(19)
["periodo"]=>
string(7) "2017-11"
["tipoOperacion"]=>
string(4) "PAGO"
["vencimiento"]=>
string(10) "2017-12-05" //IT'S NOT THE CURRENT MONTH
}
[8]=>
array(5) {
["idImpuesto"]=>
int(308)
["idConcepto"]=>
int(19)
["periodo"]=>
string(7) "2017-10"
["tipoOperacion"]=>
string(4) "PAGO"
["vencimiento"]=>
string(10) "2017-11-06"
}
}
}
如何以这种方式过滤整个阵列
array(2) {
[264]=>
array(24) {
[0]=>
array(6) {
["idImpuesto"]=>
int(10)
["idConcepto"]=>
int(19)
["periodo"]=>
string(4) "2017"
["tipoOperacion"]=>
string(12) "PRESENTACION"
["vencimiento"]=>
string(10) "2017-11-13"
["formularios"]=>
string(3) "713"
}
[1]=>
array(5) {
["idImpuesto"]=>
int(10)
["idConcepto"]=>
int(19)
["periodo"]=>
string(4) "2017"
["tipoOperacion"]=>
string(4) "PAGO"
["vencimiento"]=>
string(10) "2017-11-13"
}
[2]=>
array(6) {
["idImpuesto"]=>
int(30)
["idConcepto"]=>
int(19)
["periodo"]=>
string(7) "2017-10"
["tipoOperacion"]=>
string(12) "PRESENTACION"
["vencimiento"]=>
string(10) "2017-11-21"
["formularios"]=>
string(8) "731,2002"
}
[3]=>
array(5) {
["idImpuesto"]=>
int(30)
["idConcepto"]=>
int(19)
["periodo"]=>
string(7) "2017-10"
["tipoOperacion"]=>
string(4) "PAGO"
["vencimiento"]=>
string(10) "2017-11-21"
}
}
[265]=>
array(3) {
[0]=>
array(6) {
["idImpuesto"]=>
int(30)
["idConcepto"]=>
int(19)
["periodo"]=>
string(7) "2017-10"
["tipoOperacion"]=>
string(12) "PRESENTACION"
["vencimiento"]=>
string(10) "2017-11-22"
["formularios"]=>
string(8) "731,2002"
}
[1]=>
array(5) {
["idImpuesto"]=>
int(30)
["idConcepto"]=>
int(19)
["periodo"]=>
string(7) "2017-10"
["tipoOperacion"]=>
string(4) "PAGO"
["vencimiento"]=>
string(10) "2017-11-22"
}
[2]=>
array(5) {
["idImpuesto"]=>
int(308)
["idConcepto"]=>
int(19)
["periodo"]=>
string(7) "2017-10"
["tipoOperacion"]=>
string(4) "PAGO"
["vencimiento"]=>
string(10) "2017-11-06"
}
}
}
有什么想法吗?是你的朋友
假设您的完整数组是$array
:
$current_month = date('Y-m');
foreach($array as $key => $sub_array) {
$array[$key] = array_filter($sub_array, function($item) use($current_month) {
return substr($item['vencimiento'], 0, 7) == $current_month;
});
}
$array = array_filter($array);
他是你的朋友
假设您的完整数组是$array
:
$current_month = date('Y-m');
foreach($array as $key => $sub_array) {
$array[$key] = array_filter($sub_array, function($item) use($current_month) {
return substr($item['vencimiento'], 0, 7) == $current_month;
});
}
$array = array_filter($array);
您可以使用两个循环和
date()
来比较年份和月份:
foreach($array as &$val1) {
foreach($val1 as &$val2) {
if(date('Y-m', strtotime($val2['vencimiento'])) != date('Y-m')) {
unset($val2);
}
}
}
您可以使用两个循环和
date()
来比较年份和月份:
foreach($array as &$val1) {
foreach($val1 as &$val2) {
if(date('Y-m', strtotime($val2['vencimiento'])) != date('Y-m')) {
unset($val2);
}
}
}
您可以使用date()
$result = [];
foreach($var as $v) {
foreach($v as $a) {
$date = date('F',strtotime($a['vencimiento']));
$now = date('F');
if($date === $now) {
$result[] = $v;
}
}
}
var_dump($var);
var_dump($result);
可以使用date()
获取当前月份名称,然后将其放入一个新数组中,如:
$result = [];
foreach($var as $v) {
foreach($v as $a) {
$date = date('F',strtotime($a['vencimiento']));
$now = date('F');
if($date === $now) {
$result[] = $v;
}
}
}
var_dump($var);
var_dump($result);
此代码创建一个具有所需输出的新数组
// define new array
$new_array = array();
// get year and date in the same format as vencimiento in array
$year_month = date('Y-m');
foreach($array as $key=>$value){
foreach($value as $k=>$v){
// check for the condition
if(substr($v['vencimiento'], 0, 7) == $year_month){
// assign to new array
$new_array[$key][]=$v;
}
}
}
print_r($new_array);
这段代码创建了一个具有所需输出的新数组
// define new array
$new_array = array();
// get year and date in the same format as vencimiento in array
$year_month = date('Y-m');
foreach($array as $key=>$value){
foreach($value as $k=>$v){
// check for the condition
if(substr($v['vencimiento'], 0, 7) == $year_month){
// assign to new array
$new_array[$key][]=$v;
}
}
}
print_r($new_array);