dota220141003 2017-11-10 16:11
浏览 25
已采纳

如何用日期过滤数组?

I need to delete any where 'Vencimiento' is different from the current month

I have this array which can be variable, so it occurs to me that I must be a foreach and search if the first 6 characters of expiration ("2017-11" -xx) are equal to the current month verifying it with date ()

ARRAY:

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

How can I filter the entire array in this way?

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

any ideas?

  • 写回答

4条回答 默认 最新

  • douhu1990 2017-11-10 16:34
    关注

    This code creates a new array with desired output.

    // 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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应