Jimbo
Jimbo

Reputation: 26624

Multiple array function calls within php not returning as expected

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 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?

Upvotes: 0

Views: 95

Answers (1)

Ja͢ck
Ja͢ck

Reputation: 173642

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);
});

Upvotes: 1

Related Questions