is it possible if callback in array_filter receive parameter?

前端 未结 5 1058
小蘑菇
小蘑菇 2020-12-17 16:05

I got this multiple array named $files[], which consists of keys and values as below :

[0] => Array
(
    [name] => index1.php
    [path]          


        
5条回答
  •  旧巷少年郎
    2020-12-17 16:15

    thanks KennyTM for keyfilter class great tip. For those who care and don't know how to do it, this a working detailed example. I slightly improved using a regexp pattern.

    $files[]= {code above};

    class KeyFilter {
        function KeyFilter($attr,$pattern)
        {
            $this->attr=$attr;
            $this->pattern=$pattern;
        }
        function is_inarr_key($t)
        {
            return preg_match($this->pattern,$t[$this->attr]);;
        }
    }
    
    
    $t=array_filter($items, array(new KeyFilter("path","~\d{2}\.php~i"), 'is_inarr_key'));
    print_r($t);
    print_r($o->key);
    

    output:

    [1] => Array
    (
        [name] => index**10**.php
        [path] => http://localhost/php/gettingstarted/
        [number] => 2
    )
    
    [2] => Array
    (
        [name] => index**11**.php
        [path] => http://localhost/php/gettingstarted/
        [number] => 3
    )
    

提交回复
热议问题