我必须筛选数组$fidarray,在将日期添加到最终显示输出数组之前,要隐藏所有..ptp sync扩展名文件(即
&retarray)display fidarray is: Array
(
[0] => .ptp-sync/description
[1] => .ptp-sync/HEAD
[2] => .ptp-sync/config
[3] => .ptp-sync/COMMIT_EDITMSG
[4] => .ptp-sync/ORIG_HEAD
[5] => .ptp-sync/index
[6] => app/.ptp-sync-folder
[7] => app/genres/load.ptp-sync-folder
) // Now add the files to the retarray
foreach ($fidarray as $thisitem) {
if (strlen($thisitem) == 0) { continue; }
$stat = stat($thisitem);
$fid = substr($thisitem,$startloc);
if ($fileactions_fidfilter != "*") {
**if (preg_match('/\.ptp-sync/',$fid)) { continue; }**
$fidfilter=str_replace(".", "\.", $fileactions_fidfilter);
$fidfilter=str_replace("*", ".+", $fidfilter); 我使用了-- if (preg_match(‘/..ptp sync/’,$fid)) {连续;},它正在过滤(..ptp sync),但是我还能看到一些文件,它没有过滤所有的..ptp同步,我不知道为什么。所以我正在寻找一些替代的方法。
请提供您的支持,新编程单词,寻找简单的方法。
不是过滤-
[6] => app/.ptp-sync-folder
[7] => app/genres/load.ptp-sync-folder谢谢!
发布于 2022-12-01 16:21:58
要筛选出包含. PHP sync字符串的数组中的所有项,可以使用array_filter函数,这在PHP中是可用的。array_filter函数接受一个数组作为参数,并返回一个新数组,该数组只包含通过您指定的测试的原始数组中的项。
下面是一个示例,说明如何使用array_filter函数过滤出包含..ptp sync字符串的$fidarray中的所有项:
$fidarray = array(
".ptp-sync/description",
".ptp-sync/HEAD",
".ptp-sync/config",
".ptp-sync/COMMIT_EDITMSG",
".ptp-sync/ORIG_HEAD",
".ptp-sync/index",
"app/.ptp-sync-folder",
"app/genres/load.ptp-sync-folder",
);
// Filter out all items in $fidarray that contain the ".ptp-sync" string
$filteredArray = array_filter($fidarray, function($item) {
return !preg_match('/\.ptp-sync/', $item);
});
print_r($filteredArray);在本例中,对array_filter变量调用$fidarray函数,并传递一个回调函数,该回调函数使用preg_match函数检查$fidarray中的每个项是否包含..ptp sync字符串。如果某项包含..ptp sync字符串,则preg_match函数将返回true,而array_filter函数将从结果数组中排除该项。
https://stackoverflow.com/questions/74644814
复制相似问题