New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ability to use array keys with array_filter() #287

Merged
merged 5 commits into from Oct 30, 2013

Conversation

7 participants
@datibbaw
Copy link
Contributor

datibbaw commented Feb 21, 2013

This adds a third (optional) argument to array_filter() that will determine what gets passed to the callback, the array key, value or both.

    array_filter(array(1 => 'a', 2 => 'b', a' => 1, 'b' => 2), 'is_numeric', ARRAY_FILTER_USE_KEY);
    // returns: [1 => 'a', 2 => 'b']

    array_filter(array(1 => 'a', 2 => 'b', a' => 1, 'b' => 2), 'is_numeric');
    // returns: ['a' => 1, 'b' => 2]

    array_filter(array(1 => 'a', 2 => 'b', a' => 1, 'b' => 2), function($value, $key) {
        return is_numeric($key) && $value == 'b';
    }, ARRAY_FILTER_USE_BOTH);
    // returns: [2 => 'b']

@davidkuridza

This comment has been minimized.

Copy link

davidkuridza commented Feb 21, 2013

+1, it would really simplify my life :)

shouldn't the example provided return [5, 6, 7]?

@datibbaw

This comment has been minimized.

Copy link
Contributor

datibbaw commented Feb 21, 2013

@davidkuridza That's if array keys were 1-indexed :)

@davidkuridza

This comment has been minimized.

Copy link

davidkuridza commented Feb 21, 2013

of course, stupid me :)

@weltling

This comment has been minimized.

Copy link
Contributor

weltling commented Feb 27, 2013

Tested on windows, it's ok (except see the comment about the test).

Another thing - actually the third arg isn't needed, you could just pass two args to the func by default. If the filter callback has only one arg in the signature, so the second is just ignored. That means the older code is still compatible while no need to add the third arg to the array_filter signature.

@datibbaw

This comment has been minimized.

Copy link
Contributor

datibbaw commented Feb 27, 2013

@weltling I've discussed dropping the third arg on the mailing list; basically, functions with optional arguments may start to break if we always send the key.

@weltling

This comment has been minimized.

Copy link
Contributor

weltling commented Feb 27, 2013

True, passing two args unconditionally is a BC breach, sigh! The test is ok now, also all the other array_filter_* tests pass. Good work!

@datibbaw

This comment has been minimized.

Copy link
Contributor

datibbaw commented Feb 28, 2013

@weltling Great! I'm a bit new with this, do I close the pull request?

@weltling

This comment has been minimized.

Copy link
Contributor

weltling commented Feb 28, 2013

May be someone else will take a look yet. Please wait until it's merged then. The person who merges it will close it.

@dsp

This comment has been minimized.

Copy link
Member

dsp commented Sep 16, 2013

Thanks for your patience. Things aren't always going as fast as hoped for :/.

This thread is stalled atm :/. Can you resend a list to the ML and ping me back in 2 weeks? If there are no objections, I would like to have that change and I don't think an RFC is necessary (even though people might disagree), but I would like to see it being discussed before merging it into master.

@hikari-no-yume

This comment has been minimized.

Copy link
Contributor

hikari-no-yume commented Sep 25, 2013

Can we have the bitmask option? I like it best.

@srgoogleguy

This comment has been minimized.

Copy link
Contributor

srgoogleguy commented Oct 30, 2013

Merged to master, you can close this now

@hikari-no-yume

This comment has been minimized.

Copy link
Contributor

hikari-no-yume commented Oct 30, 2013

@srgoogleguy It looks to me as if there's a piece missing. It doesn't seem to actually define ARRAY_FILTER_USE_VALUE as a constant, it's just a fake constant as a default. That seems strange. Even if specifying it explicitly is unnecessary, it should exist as a proper constant.

@php-pulls php-pulls merged commit 75ba75e into php:master Oct 30, 2013

1 check failed

default The Travis CI build failed
Details

@datibbaw datibbaw deleted the datibbaw:array_filter_with_keys branch Oct 30, 2013

@srgoogleguy

This comment has been minimized.

Copy link
Contributor

srgoogleguy commented Oct 31, 2013

@TazeTSchnitzel If you followed the discussion on Internals you should know that the implementation has changed to not define an ARRAY_FILTER_USE_VALUE constant, since this is the default behavior anyway. It doesn't make much sense to define a constant for behavior the function does regardless of the argument supplied.

See http://news.php.net/php.internals/69341

You were probably looking at https://github.com/php/php-src/pull/287/files#diff-9a4220f442d2acbb4519beb4481d13e9R5 which mentions the constant name in the test file comment. Admittedly this may confuse a few folks. Perhaps we should delete that comment as not to confuse anyone for now.

@datibbaw

This comment has been minimized.

Copy link
Contributor

datibbaw commented Oct 31, 2013

And this is why I don't like documenting my code ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment