duanni5726 2017-10-29 22:31
浏览 22
已采纳

如何遍历和过滤数组?

how to traverse the array and filter all except the current month?

example:

i have this:

    array(1) {
  [257]=>
  array(10) {
    ["recaudacion"]=>
    array(13) {
      [0]=>
      string(10) "2017-01-07"
      [1]=>
      string(10) "2017-02-07"
      [2]=>
      string(10) "2017-03-07"
      [3]=>
      string(10) "2017-04-07"
      [4]=>
      string(10) "2017-05-07"
      [5]=>
      string(10) "2017-06-07"
      [6]=>
      string(10) "2017-07-07"
      [7]=>
      string(10) "2017-08-07"
      [8]=>
      string(10) "2017-09-07"
      [9]=>
      string(10) "2017-10-07"
      [10]=>
      string(10) "2017-11-07"
      [11]=>
      string(10) "2017-12-07"
      [12]=>
      string(10) "2018-01-07"
    }
    ["Contribuyentes Convenio Multilateral"]=>
    array(13) {
      [0]=>
      string(10) "2017-01-13"
      [1]=>
      string(10) "2017-02-13"
      [2]=>
      string(10) "2017-03-13"
      [3]=>
      string(10) "2017-04-17"
      [4]=>
      string(10) "2017-05-15"
      [5]=>
      string(10) "2017-06-13"
      [6]=>
      string(10) "2017-07-13"
      [7]=>
      string(10) "2017-08-14"
      [8]=>
      string(10) "2017-09-13"
      [9]=>
      string(10) "2017-10-13"
      [10]=>
      string(10) "2017-11-13"
      [11]=>
      string(10) "2017-12-13"
      [12]=>
      string(10) "2018-01-13"
    }
    ["Contribuyentes Locales,Activid. Especiales y Salas de Recreacion"]=>
    array(13) {
      [0]=>
      string(10) "2017-01-16"
      [1]=>
      string(10) "2017-02-15"
      [2]=>
      string(10) "2017-03-15"
      [3]=>
      string(10) "2017-04-17"
      [4]=>
      string(10) "2017-05-15"
      [5]=>
      string(10) "2017-06-15"
      [6]=>
      string(10) "2017-07-17"
      [7]=>
      string(10) "2017-08-16"
      [8]=>
      string(10) "2017-09-15"
      [9]=>
      string(10) "2017-10-16"
      [10]=>
      string(10) "2017-11-15"
      [11]=>
      string(10) "2017-12-15"
      [12]=>
      string(10) "2018-01-16"
    }
    ["Regimen simplificado"]=>
    array(13) {
      [0]=>
      string(10) "2017-01-20"
      [1]=>
      string(10) "2017-02-00"
      [2]=>
      string(10) "2017-03-20"
      [3]=>
      string(10) "2017-04-00"
      [4]=>
      string(10) "2017-05-22"
      [5]=>
      string(10) "2017-06-00"
      [6]=>
      string(10) "2017-07-20"
      [7]=>
      string(10) "2017-08-00"
      [8]=>
      string(10) "2017-09-20"
      [9]=>
      string(10) "2017-10-00"
      [10]=>
      string(10) "2017-11-20"
      [11]=>
      string(10) "2017-12-00"
      [12]=>
      string(10) "2018-01-20"
    }
    ["Declaracion Jurada Fiscal 2016 Contibuyentes Locales"]=>
    array(13) {
      [0]=>
      string(10) "2017-01-00"
      [1]=>
      string(10) "2017-02-00"
      [2]=>
      string(10) "2017-03-00"
      [3]=>
      string(10) "2017-04-00"
      [4]=>
      string(10) "2017-05-00"
      [5]=>
      string(10) "2017-06-00"
      [6]=>
      string(10) "2017-07-00"
      [7]=>
      string(10) "2017-08-00"
      [8]=>
      string(10) "2017-09-00"
      [9]=>
      string(10) "2017-10-00"
      [10]=>
      string(10) "2017-11-00"
      [11]=>
      string(10) "2017-12-00"
      [12]=>
      string(10) "2018-01-00"
    }
    ["Recategorizacion cuatrimestral regimen simplificado"]=>
    array(13) {
      [0]=>
      string(11) "2017-01-131"
      [1]=>
      string(10) "2017-02-00"
      [2]=>
      string(10) "2017-03-00"
      [3]=>
      string(10) "2017-04-00"
      [4]=>
      string(11) "2017-05-131"
      [5]=>
      string(10) "2017-06-00"
      [6]=>
      string(10) "2017-07-00"
      [7]=>
      string(10) "2017-08-00"
      [8]=>
      string(11) "2017-09-130"
      [9]=>
      string(10) "2017-10-00"
      [10]=>
      string(10) "2017-11-00"
      [11]=>
      string(10) "2017-12-00"
      [12]=>
      string(11) "2018-01-131"
    }
    ["Declaracion Jurada anual 2016 Contribuyentes de convenio multilateral"]=>
    array(13) {
      [0]=>
      string(10) "2017-01-00"
      [1]=>
      string(10) "2017-02-00"
      [2]=>
      string(10) "2017-03-00"
      [3]=>
      string(10) "2017-04-00"
      [4]=>
      string(10) "2017-05-00"
      [5]=>
      string(10) "2017-06-30"
      [6]=>
      string(10) "2017-07-00"
      [7]=>
      string(10) "2017-08-00"
      [8]=>
      string(10) "2017-09-00"
      [9]=>
      string(10) "2017-10-00"
      [10]=>
      string(10) "2017-11-00"
      [11]=>
      string(10) "2017-12-00"
      [12]=>
      string(10) "2018-01-00"
    }
    ["Impuestos de Sellos"]=>
    array(13) {
      [0]=>
      string(10) "2017-01-10"
      [1]=>
      string(10) "2017-02-10"
      [2]=>
      string(10) "2017-03-10"
      [3]=>
      string(10) "2017-04-10"
      [4]=>
      string(10) "2017-05-10"
      [5]=>
      string(10) "2017-06-10"
      [6]=>
      string(10) "2017-07-10"
      [7]=>
      string(10) "2017-08-10"
      [8]=>
      string(10) "2017-09-10"
      [9]=>
      string(10) "2017-10-10"
      [10]=>
      string(10) "2017-11-10"
      [11]=>
      string(10) "2017-12-10"
      [12]=>
      string(10) "2018-01-10"
    }
    ["Ingresos Brutos Contribuyentes Locales"]=>
    array(13) {
      [0]=>
      string(10) "21/02/2017"
      [1]=>
      string(10) "21/03/2017"
      [2]=>
      string(10) "19/04/2017"
      [3]=>
      string(10) "19/05/2017"
      [4]=>
      string(10) "21/06/2017"
      [5]=>
      string(10) "19/07/2017"
      [6]=>
      string(10) "22/08/2017"
      [7]=>
      string(10) "19/09/2017"
      [8]=>
      string(10) "19/10/2017"
      [9]=>
      string(10) "21/11/2017"
      [10]=>
      string(10) "19/12/2017"
      [11]=>
      string(10) "19/01/2018"
      [12]=>
      string(10) "04/04/2017"
    }
    ["Ingresos Brutos de Convenio Multilateral"]=>
    array(13) {
      [0]=>
      string(10) "13/02/2017"
      [1]=>
      string(10) "13/03/2017"
      [2]=>
      string(10) "17/04/2017"
      [3]=>
      string(10) "15/05/2017"
      [4]=>
      string(10) "13/06/2017"
      [5]=>
      string(10) "13/07/2017"
      [6]=>
      string(10) "14/08/2017"
      [7]=>
      string(10) "13/09/2017"
      [8]=>
      string(10) "13/10/2017"
      [9]=>
      string(10) "13/11/2017"
      [10]=>
      string(10) "13/12/2017"
      [11]=>
      string(10) "15/01/2018"
      [12]=>
      string(10) "30/06/2017"
    }
  }
}
array(1) {
  [258]=>
  array(10) {
    ["recaudacion"]=>
    array(13) {
      [0]=>
      string(10) "2017-01-07"
      [1]=>
      string(10) "2017-02-07"
      [2]=>
      string(10) "2017-03-07"
      [3]=>
      string(10) "2017-04-07"
      [4]=>
      string(10) "2017-05-07"
      [5]=>
      string(10) "2017-06-07"
      [6]=>
      string(10) "2017-07-07"
      [7]=>
      string(10) "2017-08-07"
      [8]=>
      string(10) "2017-09-07"
      [9]=>
      string(10) "2017-10-07"
      [10]=>
      string(10) "2017-11-07"
      [11]=>
      string(10) "2017-12-07"
      [12]=>
      string(10) "2018-01-07"
    }
    ["Contribuyentes Convenio Multilateral"]=>
    array(13) {
      [0]=>
      string(10) "2017-01-13"
      [1]=>
      string(10) "2017-02-13"
      [2]=>
      string(10) "2017-03-13"
      [3]=>
      string(10) "2017-04-17"
      [4]=>
      string(10) "2017-05-15"
      [5]=>
      string(10) "2017-06-13"
      [6]=>
      string(10) "2017-07-13"
      [7]=>
      string(10) "2017-08-14"
      [8]=>
      string(10) "2017-09-13"
      [9]=>
      string(10) "2017-10-13"
      [10]=>
      string(10) "2017-11-13"
      [11]=>
      string(10) "2017-12-13"
      [12]=>
      string(10) "2018-01-13"
    }
    ["Contribuyentes Locales,Activid. Especiales y Salas de Recreacion"]=>
    array(13) {
      [0]=>
      string(10) "2017-01-17"
      [1]=>
      string(10) "2017-02-16"
      [2]=>
      string(10) "2017-03-16"
      [3]=>
      string(10) "2017-04-18"
      [4]=>
      string(10) "2017-05-16"
      [5]=>
      string(10) "2017-06-16"
      [6]=>
      string(10) "2017-07-18"
      [7]=>
      string(10) "2017-08-17"
      [8]=>
      string(10) "2017-09-18"
      [9]=>
      string(10) "2017-10-17"
      [10]=>
      string(10) "2017-11-16"
      [11]=>
      string(10) "2017-12-18"
      [12]=>
      string(10) "2018-01-17"
    }
    ["Regimen simplificado"]=>
    array(13) {
      [0]=>
      string(10) "2017-01-23"
      [1]=>
      string(10) "2017-02-00"
      [2]=>
      string(10) "2017-03-21"
      [3]=>
      string(10) "2017-04-00"
      [4]=>
      string(10) "2017-05-23"
      [5]=>
      string(10) "2017-06-00"
      [6]=>
      string(10) "2017-07-21"
      [7]=>
      string(10) "2017-08-00"
      [8]=>
      string(10) "2017-09-21"
      [9]=>
      string(10) "2017-10-00"
      [10]=>
      string(10) "2017-11-21"
      [11]=>
      string(10) "2017-12-00"
      [12]=>
      string(10) "2018-01-23"
    }
    ["Declaracion Jurada Fiscal 2016 Contibuyentes Locales"]=>
    array(13) {
      [0]=>
      string(10) "2017-01-00"
      [1]=>
      string(10) "2017-02-00"
      [2]=>
      string(10) "2017-03-00"
      [3]=>
      string(10) "2017-04-00"
      [4]=>
      string(10) "2017-05-00"
      [5]=>
      string(10) "2017-06-00"
      [6]=>
      string(10) "2017-07-00"
      [7]=>
      string(10) "2017-08-00"
      [8]=>
      string(10) "2017-09-00"
      [9]=>
      string(10) "2017-10-00"
      [10]=>
      string(10) "2017-11-00"
      [11]=>
      string(10) "2017-12-00"
      [12]=>
      string(10) "2018-01-00"
    }
    ["Recategorizacion cuatrimestral regimen simplificado"]=>
    array(13) {
      [0]=>
      string(11) "2017-01-131"
      [1]=>
      string(10) "2017-02-00"
      [2]=>
      string(10) "2017-03-00"
      [3]=>
      string(10) "2017-04-00"
      [4]=>
      string(11) "2017-05-131"
      [5]=>
      string(10) "2017-06-00"
      [6]=>
      string(10) "2017-07-00"
      [7]=>
      string(10) "2017-08-00"
      [8]=>
      string(11) "2017-09-130"
      [9]=>
      string(10) "2017-10-00"
      [10]=>
      string(10) "2017-11-00"
      [11]=>
      string(10) "2017-12-00"
      [12]=>
      string(11) "2018-01-131"
    }
    ["Declaracion Jurada anual 2016 Contribuyentes de convenio multilateral"]=>
    array(13) {
      [0]=>
      string(10) "2017-01-00"
      [1]=>
      string(10) "2017-02-00"
      [2]=>
      string(10) "2017-03-00"
      [3]=>
      string(10) "2017-04-00"
      [4]=>
      string(10) "2017-05-00"
      [5]=>
      string(10) "2017-06-30"
      [6]=>
      string(10) "2017-07-00"
      [7]=>
      string(10) "2017-08-00"
      [8]=>
      string(10) "2017-09-00"
      [9]=>
      string(10) "2017-10-00"
      [10]=>
      string(10) "2017-11-00"
      [11]=>
      string(10) "2017-12-00"
      [12]=>
      string(10) "2018-01-00"
    }
    ["Impuestos de Sellos"]=>
    array(13) {
      [0]=>
      string(10) "2017-01-10"
      [1]=>
      string(10) "2017-02-10"
      [2]=>
      string(10) "2017-03-10"
      [3]=>
      string(10) "2017-04-10"
      [4]=>
      string(10) "2017-05-10"
      [5]=>
      string(10) "2017-06-10"
      [6]=>
      string(10) "2017-07-10"
      [7]=>
      string(10) "2017-08-10"
      [8]=>
      string(10) "2017-09-10"
      [9]=>
      string(10) "2017-10-10"
      [10]=>
      string(10) "2017-11-10"
      [11]=>
      string(10) "2017-12-10"
      [12]=>
      string(10) "2018-01-10"
    }
    ["Ingresos Brutos Contribuyentes Locales"]=>
    array(13) {
      [0]=>
      string(10) "22/02/2017"
      [1]=>
      string(10) "22/03/2017"
      [2]=>
      string(10) "20/04/2017"
      [3]=>
      string(10) "22/05/2017"
      [4]=>
      string(10) "22/06/2017"
      [5]=>
      string(10) "20/07/2017"
      [6]=>
      string(10) "23/08/2017"
      [7]=>
      string(10) "20/09/2017"
      [8]=>
      string(10) "20/10/2017"
      [9]=>
      string(10) "22/11/2017"
      [10]=>
      string(10) "20/12/2017"
      [11]=>
      string(10) "22/01/2018"
      [12]=>
      string(10) "05/04/2017"
    }
    ["Ingresos Brutos de Convenio Multilateral"]=>
    array(13) {
      [0]=>
      string(10) "13/02/2017"
      [1]=>
      string(10) "13/03/2017"
      [2]=>
      string(10) "17/04/2017"
      [3]=>
      string(10) "15/05/2017"
      [4]=>
      string(10) "13/06/2017"
      [5]=>
      string(10) "13/07/2017"
      [6]=>
      string(10) "14/08/2017"
      [7]=>
      string(10) "13/09/2017"
      [8]=>
      string(10) "13/10/2017"
      [9]=>
      string(10) "13/11/2017"
      [10]=>
      string(10) "13/12/2017"
      [11]=>
      string(10) "15/01/2018"
      [12]=>
      string(10) "30/06/2017"
    }
  }
}

and i want this:

array(1) {
  [257]=>
  array(10) {
    ["recaudacion"]=>
    array(13) {
      [0]=>
      string(10) "2017-10-07"
    }
    ["Contribuyentes Convenio Multilateral"]=>
    array(13) {
      [0]=>
      string(10) "2017-10-13"
    }
    ["Contribuyentes Locales,Activid. Especiales y Salas de Recreacion"]=>
    array(13) {
      [0]=>
      string(10) "2017-10-16"
    }
    ["Regimen simplificado"]=>
    array(13) {
      [0]=>
      string(10) "2017-10-00"
    }
    ["Declaracion Jurada Fiscal 2016 Contibuyentes Locales"]=>
    array(13) {
      [0]=>
      string(10) "2017-10-00"
    }
    ["Recategorizacion cuatrimestral regimen simplificado"]=>
    array(13) {
      [0]=>
      string(10) "2017-10-00"
    }
    ["Declaracion Jurada anual 2016 Contribuyentes de convenio multilateral"]=>
    array(13) {
      [0]=>
      string(10) "2017-10-00"
    }
    ["Impuestos de Sellos"]=>
    array(13) {
      [0]=>
      string(10) "2017-10-10"
    }
    ["Ingresos Brutos Contribuyentes Locales"]=>
    array(13) {
      [0]=>
      string(10) "19/10/2017"
    }
    ["Ingresos Brutos de Convenio Multilateral"]=>
    array(13) {
      [0]=>
      string(10) "13/10/2017"
    }
  }
}
array(1) {
  [258]=>
  array(10) {
    ["recaudacion"]=>
    array(13) {
      [0]=>
      string(10) "2017-10-07"
    }
    ["Contribuyentes Convenio Multilateral"]=>
    array(13) {
      [0]=>
      string(10) "2017-10-13"
    }
    ["Contribuyentes Locales,Activid. Especiales y Salas de Recreacion"]=>
    array(13) {
      [0]=>
      string(10) "2017-10-17"
    }
    ["Regimen simplificado"]=>
    array(13) {
      [0]=>
      string(10) "2017-10-00"
    }
    ["Declaracion Jurada Fiscal 2016 Contibuyentes Locales"]=>
    array(13) {
      [0]=>
      string(10) "2017-10-00"
    }
    ["Recategorizacion cuatrimestral regimen simplificado"]=>
    array(13) {
      [0]=>
      string(10) "2017-10-00"
    }
    ["Declaracion Jurada anual 2016 Contribuyentes de convenio multilateral"]=>
    array(13) {
      [0]=>
      string(10) "2017-10-00"
    }
    ["Impuestos de Sellos"]=>
    array(13) {
      [0]=>
      string(10) "2017-10-10"
    }
    ["Ingresos Brutos Contribuyentes Locales"]=>
    array(13) {
      [0]=>
      string(10) "20/10/2017"
    }
    ["Ingresos Brutos de Convenio Multilateral"]=>
    array(13) {
      [0]=>
      string(10) "13/10/2017"
    }
  }
}

I do not care about the day but it must be kept.

my code here:

function filtrarVencimientos($arrayCompleto){

    $filtered = array_filter($arrayCompleto, 'month');

var_dump($filtered);
    return $filtered;
}

function month($outer) {
  $dt = new \DateTime;
$current_month = $dt->format('Y-m');

foreach ($outer as &$middle) {
    foreach ($middle as &$inner) {
        foreach ($inner as &$item) {
            if (date_format($item, 'Y-m') == $current_month) {
              unset($item);
            }
            return $item;
        }


    }


  }

}

function getFiltrado(){
  $arrayCompleto = getVencimientosPorPerfil();

  $arrayFiltrado = filtrarVencimientos($arrayCompleto);

  return $arrayFiltrado;
}

I tried using stripos but it does not support arrays and I could not think of a way to do it ... any ideas?

I edited the question by adding my code and the complete result of the original array

  • 写回答

2条回答 默认 最新

  • dongpiaozhao6836 2017-10-30 00:28
    关注

    Loop up to dates, compare to current date, unset if false

    foreach($array as $key1 => $outer) {
        foreach($outer as $key2 => $inner) {
            foreach ($inner as $key3 => $date) {
                // Added substr function to replace 00 Days to 01
                if (substr($date, 8, 2) === '00') {
                    $date = str_replace('00', '01', $date);
                } // End of Edit
                $date = strtotime($date);
                if (date('m', $date) !== date('m')) {
                    unset($array[$key1][$key2][$key3]);
                }
                // Added new if condition to remove empty inner arrays
                if (count($array[$key1][$key2]) === 0 ) {
                    unset($array[$key1][$key2]);
                } // End of Edit
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?