Skip to content

Conversation

@danieliser
Copy link

Similar to extract except that it doesnt return keys that arnt set in the first array.

EX..
Arr::filter(
array('a' => 'foo', 'b' => 'bar'),
array('a','c')
);
Returns array('a' => 'foo')
Key b is removed from results altogether.

This is particularly useful for extracting fields from $_POST for Model without leaveing unset values as NULL.
Regular extract in this situation will end up saving value "C" above as NULL even if your only attempting to change "A".


Similar to extract except that it doesnt return keys that arnt set in the first array.

EX..
Arr::filter(
array('a' => 'foo', 'b' => 'bar'),
array('a','c')
);
Returns array('a' => 'foo')
Key b is removed from results altogether.

This is particularly useful for extracting fields from $_POST for Model without leaveing unset values as NULL.
Regular extract in this situation will end up saving value "C" above as NULL even if your only attempting to change "A".
@zeelot
Copy link
Member

zeelot commented Jul 11, 2013

Did you ever make a redmine ticket for this?

@cs278
Copy link
Contributor

cs278 commented Jul 11, 2013

Bit of a mouthful, Underscore calls this pick.

You could rewrite the method to:

array_intersect_key($arr, array_flip($keys))

But this won't work on objects that implement \Traversable and \ArrayAccess, and it'll handle nulls differently.

@enov
Copy link
Contributor

enov commented Nov 21, 2014

Thanks for this @danieliser. No need for a Redmine ticket anymore, but definitely unit tests :)

@enov
Copy link
Contributor

enov commented Nov 21, 2014

Also, I think the method name needs to change, but I am not sure.

@enov enov added this to the 3.4.0 milestone Nov 21, 2014
@acoulton
Copy link
Member

This would be really useful - I agree with @enov and @cs278, pick might be a better name.

Though if this were a blank sheet, I'd call this extract and have called the existing method something else, since it adds extra values to the result....

@danieliser
Copy link
Author

Agreed, i thought of several names since.
pick, fetch, only, pull.

Glad to see this finally make it into core. Hell glad to see that project isn't dead. Felt like community died shortly after i started using Kohana heavily.

@neo22s
Copy link
Member

neo22s commented Mar 21, 2016

I like this, missing tests, should be for 4.0.0?

@neo22s neo22s modified the milestones: 4.0.0, 3.4.0 Mar 21, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants