首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从数组中隐藏关键字

从数组中隐藏关键字
EN

Stack Overflow用户
提问于 2022-12-01 16:02:25
回答 1查看 30关注 0票数 0

我必须筛选数组$fidarray,在将日期添加到最终显示输出数组之前,要隐藏所有..ptp sync扩展名文件(即

代码语言:javascript
复制
&retarray)
代码语言:javascript
复制
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
)
代码语言:javascript
复制
                // 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同步,我不知道为什么。所以我正在寻找一些替代的方法。

请提供您的支持,新编程单词,寻找简单的方法。

不是过滤-

代码语言:javascript
复制
    [6] =>  app/.ptp-sync-folder
    [7] =>  app/genres/load.ptp-sync-folder

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2022-12-01 16:21:58

要筛选出包含. PHP sync字符串的数组中的所有项,可以使用array_filter函数,这在PHP中是可用的。array_filter函数接受一个数组作为参数,并返回一个新数组,该数组只包含通过您指定的测试的原始数组中的项。

下面是一个示例,说明如何使用array_filter函数过滤出包含..ptp sync字符串的$fidarray中的所有项:

代码语言:javascript
复制
$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函数将从结果数组中排除该项。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74644814

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档