繁体   English   中英

使用通配符从php数组中删除特定键

[英]remove specific keys from php array with wildcard

我试图在数组中搜索特定的部分字符串,并删除包含它们的所有键。 我已经尝试了几件事,但我的PHP-fu在这里用完了...

我正在搜索的数组(部分但沿着这些方向很多)是这样的:

Array
(
    [0] => /.
    [1] => /..
    [2] => /htdocs
    [3] => /htdocs/.
    [4] => /htdocs/..
    [5] => /htdocs/zipper_new.php
    [6] => /htdocs/bottom.gif
)

我正在尝试删除所有包含“ /”的条目。 和“ / ..”。

总而言之,我制作了一个脚本来按文件类型搜索文件并将其压缩,我正在尝试添加功能来代替搜索所有文件并执行相同的操作,这部分失败了,引用了包含那些字符串的条目无论是全部还是部分。

$data = array(
    '/.',
    '/..',
    '/htdocs',
    '/htdocs/.',
    '/htdocs/..',
    '/htdocs/zipper_new.php',
    '/htdocs/bottom.gif',
);

$match = '/.';

$newData = array_filter(
    $data,
    function($value) use ($match) {
        return (!fnmatch('*' . $match . '*', $value));
    }
);

var_dump($newData);

您不能简单地使用foreach循环并使用诸如strstr之类的函数来查找字符串吗?

然后,使用unset删除该条目。

foreach ($table as $key => $item) {
    if (strstr($item, '/.')) // If it contains /. it will also match for /..
        unset($table[$key]);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM