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 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助