如何在array_map中添加多个回调
<?php
$scandir = scandir('.');
echo "<pre>";
print_r($scandir);
$scandir = array_map('array_filter',array_map('callback',$scandir));
print_r($scandir);
function callback(&$var){
$curtime = time();
$filetime = filemtime($var);
$diff = $curtime - $filetime;
if ($diff < 2000){
return $var;
}
}
?>
实际输出
Array
(
[0] => .
[1] => ..
[2] => array.php
[3] => array.php.bak
[4] => code-test.php
[5] => code-test.txt
[6] => code-test.txt.bak
[7] => code-test2.php
[8] => load-file.csv
[9] => load-file2.csv
)
Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
)
预期产出
Array
(
[2] => array.php
[3] => array.php.bak
)
答案 0 :(得分:1)
我认为问题出在你打电话时
<?php
$scandir = scandir('.');
echo "<pre>";
print_r($scandir);
$scandir = array_filter($scandir,'callback');
print_r($scandir);
function callback(&$var){
$curtime = time();
$filetime = filemtime($var);
$diff = $curtime - $filetime;
if ($diff < 2000 && $var != '.'){
return $var;
}
}
?>
因为您可以使用array_filter函数来实现这一目标。
以下内容应该有效
{{1}}
答案 1 :(得分:1)
有点不清楚你真正想要做什么,虽然如果目的是检查从现在起在一定范围内修改过的文件或目录,那么对你的代码进行一些调整就足够了:
function callback(&$var) {
$curtime = time();
$filetime = filemtime($var);
$diff = $curtime - $filetime;
if ($diff < 2000) {
return $var;
}
};
$scandir = scandir('.');
$scandir = array_filter(array_map("callback", $scandir));
print_r($scandir);
您可能没有必要使用某些代码,但遗憾的是我无法检查它。