douci2516 2013-10-08 10:16
浏览 33
已采纳

php中的多个数组函数调用未按预期返回

Note: This is a question about a technical exercise. Please focus on utilising the same or similar array functions that I require instead of separating the code out into obviously more readable, but longer code segments.

I'm attempting to use an array_filter() and an array_walk() together. I haven't used these functions much, so I would like to learn about them in the process.

  • I have an array of possible environment strings ('sandbox', 'live', 'dev')
  • I have a string containing chosen environment ('dev')
  • I have an array of filepaths using glob()

I need to retrieve a list of files, using glob, that don't contain the strings from the difference between the two environments.

I have the following starting variables:

$possible = array('dev', 'sandbox', 'live');
$env = array('dev');

And obviously the difference between the two:

$diff = array_diff($possible, $env); // array('sandbox', 'live')

The directory I'm trying to perform a glob() on contains the following:

array(
    0 => '/config/dev.yml',
    1 => '/config/sandbox.yml',
    2 => '/config/live.yml',
    3 => '/config/global.yml',
    4 => '/config/routes.yml',
    5 => '/config/security.yml'
);

I would like to use array function calls to return the following:

array(
    0 => '/config/dev.yml', // Because $env = 'dev'. Sandbox and live are removed
    1 => '/config/global.yml',
    2 => '/config/routes.yml',
    3 => '/config/security.yml
);

Notice how sandbox.yml and live.yml have been removed.

Here is the code I'm using:

/** Loop through the list of yml files, placing $file into the local scope each time around, also import $possible, $env, $diff **/
$result = array_filter(glob(dirname(__DIR__) . '/config/*.yml'), function ($file) use ($possible, $env, $diff) {
    /** Loop around the diff (sandbox, live), placing them into $d each time around, also import $file again **/
    return array_walk($diff, function($d) use ($file) {
        /** Return when each environment (sandbox, live) is NOT in the $file string **/
        return !strstr($file, $d); 
    });
});

My issue is: Whatever the inner-most return changes to, it doesn't change the result. I can return true or false in there and it doesn't make a difference. I get back a list of all the files, *including the one's containing the string "sandbox.yml" and "live.yml", which isn't what I want.

Where is my logic going wrong?

  • 写回答

1条回答 默认 最新

  • dongyun6835 2013-10-08 10:34
    关注

    The function array_walk() returns true on success which is almost every time you call it, so nothing gets filtered. You could consider a simple foreach inside instead:

    $result = array_filter($files, function ($file) use ($diff) {
        foreach ($diff as $env) {
            if (strstr($file, $env) !== false) {
                return false;
            }
        }
        return true;
    });
    

    Alternatively, as mentioned by yourself:

    $result = array_filter($files, function ($file) use ($diff) {
        return !in_array(basename($file, '.yml'), $diff);
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?