is it possible if callback in array_filter receive parameter?

前端 未结 5 1049
小蘑菇
小蘑菇 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
    )
    
    0 讨论(0)
  • 2020-12-17 16:16

    You can use create_function() in 5.2.x

    $y = 5;
    $func = 'return $x > ' . $y . ';';
    $f = create_function('$x', $func);
    $numbers = range(0, 10);
    $result = array_filter($numbers, $f);
    print_r($result);
    

    which outputs

    Array ( [6] => 6 [7] => 7 [8] => 8 [9] => 9 [10] => 10 )

    0 讨论(0)
  • 2020-12-17 16:20

    I am unaware if you can supply the callback function with parameters, but as a suggestion, you could define a callback function for array_filter

    array_filter($files, "is_inarr_key");
    

    And to get the name of the array, just loop over the $file[] and retrieve the name. Then, having the name you can go on with your logic.

    HTH.

    0 讨论(0)
  • 2020-12-17 16:26

    You can make use of the array_walk function as:

    $arr = array(
            array("name" => "Jack", "id" => 1),
            array("name" => "Rose", "id" => 2),
        );
    
    $result = array(); // initialize result array.
    array_walk($arr, "filter"); // iterate over the array calling filter fun for each value.
    // $result will now have elements "Jack" and "Rose"
    
    function filter($a) {
    
        global $result; // to access the global result array.
    
        $result[] = $a['name']; // for each array passed, save the value for the key 'name' in the result array.
    }
    
    0 讨论(0)
  • 2020-12-17 16:27

    You can create a closure on PHP ≥5.3.

    array_filter($files, function($array) use ($key) {
      return is_inarr_key($array, $key); 
    } );
    

    If you are stuck with PHP <5.3, …

    You can make $key a global variable.

    function is_inarr_with_global_key($array) {
       global $key;
       ....
    }
    

    You can create a class

    class KeyFilter {
      function KeyFilter($key) { $this->key = $key; }
      function is_inarr_key($array) { ... }
    }
    ...
    array_filter($files, array(new KeyFilter('name'), 'is_inarr_key'));
    

    You can create 3 different functions

    array_filter($files, 'is_inarr_name');
    array_filter($files, 'is_inarr_path');
    array_filter($files, 'is_inarr_number');
    

    You can write your own array_filter

    function my_array_filter($files, $key) {
      ...
    }
    
    0 讨论(0)
提交回复
热议问题