如何遍历和过滤数组?

时间:2017-10-29 22:31:56

标签: php arrays

如何遍历数组并过滤除当前月份以外的所有数据?

  

示例:

我有这个:

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

我想要这个:

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

我不关心这一天,但必须保留。

我的代码在这里:

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

我尝试使用stripo但是它不支持数组,我想不出办法做任何想法?

     

我通过添加代码和原始数组的完整结果来编辑问题

2 个答案:

答案 0 :(得分:2)

循环到日期,与当前日期比较,如果为假则取消设置

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 :(得分:1)

它将是嵌套的foreach循环,并检查当前月份,如下所示:

$dt = new \DateTime;
$current_month = $dt->format('Ym');

foreach ($outer as &$middle) {
    foreach ($middle as &$inner) {
        foreach ($inner as &$item) {
            if (date_format($item, 'Ym') == $current_month) {
                unset($item)
            }
        }

        unset($item); // 5.6 compat
    }

    unset($middle); // 5.6 compat
}

unset($inner); // 5.6 compat

如果使用PHP 7.0 or greater,您可以移除5.6 compat unset来电。