0

我的数组如下所示:

Array
(
[1] => Array
    (
        [0] => /webmail/
        [1] => 42024
        [2] => 246538196
        [3] => 72
        [4] => 70
    )

[2] => Array
    (
        [0] => /public/index.php
        [1] => 6022
        [2] => 2575777
        [3] => 40
        [4] => 153
    )

[3] => Array
    (
        [0] => /
        [1] => 293
        [2] => 5326453
        [3] => 184
        [4] => 76
    )

[4] => Array
    (
        [0] => /webmail/skins/larry/watermark.html
        [1] => 248
        [2] => 40225
        [3] => 0
        [4] => 2
    )

[5] => Array
    (
        [0] => /webmail/program/resources/blank.tif
        [1] => 182
        [2] => 29406
        [3] => 0
        [4] => 1
    )

等等...当我想删除以 .tif/.js/.css/.woff 结尾的每个元素时,我只使用:

$key = array_search('*.tif',$sider); // for example for .tif
unset($sider[$key]);

但它不起作用!我的代码中有什么问题以及如何删除多个条件,例如所有以.tif/ .js/ .css/ .woff 结尾的字符串。

谢谢

4

4 回答 4

4

array_search不起作用,因为数组的每个元素都是一个数组,并且一个数组与“*.tif”不匹配。此外,array_search不支持通配符,如*. 数组过滤器将是这里的典型方法:

$sider = array_filter($sider, function (array $element) {
    return !preg_match('/\.tif$/i', $element[0]);
});
于 2014-02-20T09:53:10.523 回答
1
<?php

function removeDeep($array, $pattern) {

    $r = array();

    if (!is_array($array)) {
        trigger_error(__FUNCTION__ . ' expected paramater 1 to be an array.', E_USER_WARNING);
        return false;
    }

    foreach ($array as $key => $value) {

        if (is_array($value)) {
            $r[$key] = call_user_func(__FUNCTION__, $value, $pattern);
            continue;
        }

        if (!preg_match($pattern, $value)) {
            $r[$key] = $value;
        }

    }

    return $r;

} 

$sider = removeDeep( $sider, "*.tif");
?> 
于 2014-02-20T10:00:42.787 回答
1

循环遍历这些值,如果扩展名在不允许的扩展名数组中,则取消设置元素。

$remove_extensions = array('tif', 'js');
foreach($arr as $key => $value) {
    if(in_array(pathinfo($value[0], PATHINFO_EXTENSION), $remove_extensions)) { unset($arr[$key]); }
}
于 2014-02-20T09:54:53.707 回答
0

试试看

var_dump(
    array_filter(
        $array,
        function ($row) {
            return preg_match('/\.(tif|css|js|woff))$/i', $row[0]) === 1;
        }
    )
);
于 2014-02-20T09:56:16.317 回答