我正在编写一个PHP函数,该函数接受一个文件名数组,如果文件名与用户输入的一组条件不匹配,则从该数组中删除文件名。该函数遍历数组并将每个值与一个正则表达式进行比较。通过从用户输入中插入变量来形成正则表达式。如果用户未指定变量,则将正则表达式通配符插入变量的位置。文件名都非常有系统性,例如2020-06-N-1.txt
,因此我确切地知道文件名中以及从用户输入中期望有多少个字符。但是,当我运行代码时,与正则表达式不匹配的文件名仍在数组中。删除了一些不匹配的文件名,但留下了许多其他文件名。下面是我的PHP代码的一部分。任何帮助表示赞赏。
function fileFilter() {
global $fileArray, $fileFilterPattern;
/* The loop starts at 2 and goes to count()-1 because the first 2 elements were removed
earlier with unset */
for ($j = 2; $j < count($fileArray) - 1; $j++) {
if(!(preg_match($fileFilterPattern, $fileArray[$j]))) {
unset($fileArray[$j]);
}
}
return;
}
// If user does not provide a filter value, it gets converted into wildcard symbol
if ($month == '') {
$month = '..';
}
if ($year == '') {
$year = '....';
}
if ($section == '') {
$section = '.';
}
$fileFilterPattern = "/{$year}-{$month}-{$section}-.\.txt/";
/* function only runs if user applied at least one filter */
if (!($month == '..' && $year == '....' && $section == '.')) {
fileFilter();
}
UPDATE下面,我提供了一个数组如何包含不匹配元素的示例。我使用echo json_encode($fileArray);
我的输入:月是“”年份是“”部分为“ L”
预期结果:数组仅包含在节中具有L的文件(YEAR-MONTH-SECTION-NUMBER.txt)
结果数组:{“ 8”:“ 2020-06-L-1.txt”,“ 9”:“ 2020-06-L-2.txt”,“ 10”:“ 2020-06-L-3.txt”,“ 11“:” 2020-06-L-4.txt“,” 12“:” 2020-06-L-5.txt“,” 15“:” 2020-06-N-3.txt“,” 16“ :“ 2020-06-N-4.txt”,“ 17”:“ 2020-06-N-5.txt”,“ 18”:“ 2020-06-N-6.txt”,“ 19”:“ 2020-06-O-1.txt“,” 20“:” 2020-06-O-2.txt“,” 21“:” 2020-06-O-3.txt“,” 22“:” 2020- 06-O-4.txt“,” 23“:” 2020-06-S-1.txt“,” 24“:” 2020-06-S-2.txt“,” 25“:” 2020-06- S-3.txt“}
问题是在循环内使用unset()。在下一次迭代中,索引不再与使用unset()弄乱数组之前的索引相同。有时,您可以使用array_values()处理此问题,但在这种情况下,仅构建仅包含所需值的第二个数组会更简单。以下代码有效。我使用array_values()只是为了获取您提供的字符串并使索引恢复正常。
就是说,因为“ 前2个元素已删除更早的时候未设置“”,您需要先在数组上运行array_values(),然后才能进入此部分。
<?php
$str ='{"8":"2020-06-L-1.txt","9":"2020-06-L-2.txt","10":"2020-06-L-3.txt","11":"2020-06-L-4.txt","12":"2020-06-L-5.txt","15":"2020-06-N-3.txt","16":"2020-06-N-4.txt","17":"2020-06-N-5.txt","18":"2020-06-N-6.txt","19":"2020-06-O-1.txt","20":"2020-06-O-2.txt","21":"2020-06-O-3.txt","22":"2020-06-O-4.txt","23":"2020-06-S-1.txt","24":"2020-06-S-2.txt","25":"2020-06-S-3.txt"}';
$fileArray = json_decode($str, true);
$fileArray = array_values($fileArray);
echo '<p>fileArray: ';
var_dump($fileArray);
echo '</p>';
function fileFilter() {
global $fileArray, $fileFilterPattern;
$filteredArray = [];
for ($j = 0; $j < count($fileArray); $j++) {
if(preg_match($fileFilterPattern, $fileArray[$j]) === 1) {
//unset($fileArray[$j]);
array_push($filteredArray, $fileArray[$j]);
}
}
echo '<p>filteredArray: ';
var_dump($filteredArray);
echo '</p>';
//return;
}
$month =='';
$year = '';
// If user does not provide a filter value, it gets converted into wildcard symbol
if ($month == '') {
$month = '..';
}
if ($year == '') {
$year = '....';
}
if ($section == '') {
$section = '.';
}
$section = 'L';
$fileFilterPattern = "#{$year}-{$month}-{$section}-.\.txt#";
echo '<p>fileFilterPattern: ';
var_dump($fileFilterPattern);
echo '</p>';
/* function only runs if user applied at least one filter */
if (!($month == '..' && $year == '....' && $section == '.')) {
fileFilter();
}
?>