在PHP中映射过滤的数组[重复]

I am trying to map one array, array1, as key to another array, array2. Basically this:

[
    "array1_val1" => [ 
        "array2_val1"=> "creation_time", 
        "array2_val2"=> "creation_time", 
        ... 
    ],
    ...,
]

This is not the problem, the problem is the method I am trying out. As follows:

$div = array_map(function ($value){
    global $files;
    global $value;
    return array_filter($files, function($k){
        // global $value;
        $ex = explode("_", $k);
        var_dump(substr($ex[0], 0, 5)."    ".$value."   ".__LINE__);
        return substr($ex[0], 0, 5) === $value;
    }, ARRAY_FILTER_USE_KEY);
}, $file_names_g);

Definitions:

  • $file_names_g - is an array that has all distinct values (first 5 letters of the file names of $files values)
  • $files - is an array with filename (key) and creation date (value). The filname starts with same 5 letters in $file_names_g and rest differ depending on date the files are created.

Filename example: ABC_24May2017.bak

Now I want the files that start with the same first 5 letters in $file_names_g with $file_names_g being a key and and array with file name as key and creation date as value, (this is the format of the $files array).

Now the problem is that I cannot figure out how to give $value variable access to the function in array_filter. Mentioning it global doesn't help, I get either an empty value or a null. How can I overcome that or is there any better method?

Regards

</div>

You can use a variable defined in the outer scope inside a closure function with use:

$value = 'foo';
function($k) use ($value) {
     // you can now access the variable value
}