Skip to content

Wildcard support for array_get #1134

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

Closed
wants to merge 4 commits into from
Closed

Wildcard support for array_get #1134

wants to merge 4 commits into from

Conversation

KaneCohen
Copy link
Contributor

Add ability to fetch items from the array with wildcard "*".

Example in the picture (and in the test).

array_get

@taylorotwell
Copy link
Member

This is quite complicated. Does underscore.php or some other lib already offer this?

@KaneCohen
Copy link
Contributor Author

@taylorotwell, no idea. I've checked google before starting to write this thing, but haven't found anything adequate (mostly when it comes to search it's about searching for keys/values with wildcard).

Which underscore? Underscore.php?

@bencorlett
Copy link
Contributor

This is cool, quite cool. But, as Otwell said, does underscore already handle this? I'd also be interested on the performance implications on non wildcard arrays

@KaneCohen
Copy link
Contributor Author

@bencorlett, don't think performance should be an issue. There's an if clause run on every key segment which checks if current segment is a wildcard. So, if it's a non wildcard key (i assume you meant that?) it should run maybe tiny little bit slower then current array_get. I'll add benchmarks later.

Checked Underscore.php (link above), but haven't found anything similar.

@taylorotwell
Copy link
Member

I think we might want to implement this as a different method separate from array_get. I do think it's a pretty nifty method, but it adds quite a bit of complexity to the array_get function.

@KaneCohen
Copy link
Contributor Author

Sure. I guess since array_get is one of the most used functions across the
framework it does have to be as fast as possible.
On May 5, 2013 4:23 AM, "Taylor Otwell" notifications@github.com wrote:

I think we might want to implement this as a different method separate
from array_get. I do think it's a pretty nifty method, but it adds quite a
bit of complexity to the array_get function.


Reply to this email directly or view it on GitHubhttps://github.com//pull/1134#issuecomment-17443688
.

@taylorotwell
Copy link
Member

Yeah it is used quite a bit since it used by configuration, etc.

@KaneCohen
Copy link
Contributor Author

@bencorlett, ok, made a little benchmark to see the difference in speed between array_get with wildcard support and without. Tested on an Atom-powered netbook.

array_get with wildcard support.

array_get($array, 'users.0.name');

Loop with 1000 iterations: 0.02089 s
Loop with 100 iterations: 0.00213 s

array_get($array, 'users.*.name');
// will return array containing two names!

Loop with 1000 iterations: 0.04872 s
Loop with 100 iterations: 0.00503 s


array_get without wildcard support.

array_get($array, 'users.0.name');

Loop with 1000 iterations: 0.01531 s
Loop with 100 iterations: 0.00152 s


So, difference between 1000 iteration of users.0.name is 5 ms.


@taylorotwell

And about name. Maybe array_wget (wild get)? Although, it might be a little bit misleading since there's a wget program.

@taylorotwell
Copy link
Member

array_select maybe?

@bencorlett
Copy link
Contributor

Go to sleep Taylor! Lol

Sent from my iPhone
Please excuse my brevity

On 14/05/2013, at 4:18 PM, Taylor Otwell notifications@github.com wrote:

array_select maybe?


Reply to this email directly or view it on GitHub.

@bencorlett
Copy link
Contributor

I like.

Sent from my iPhone
Please excuse my brevity

On 14/05/2013, at 4:18 PM, Taylor Otwell notifications@github.com wrote:

array_select maybe?


Reply to this email directly or view it on GitHub.

@KaneCohen
Copy link
Contributor Author

Huh. Found myself trying to use wildcard with Input::get('*.body'); I'm thinking maybe keep it in array_get instead of renaming? I think performance trade off is pretty reasonable (~6ms or less on netbook running 10k iterations) considering functionality that thing adds. Besides that, without renaming it'll be immediately available for use with Input, File, Config and other calls

@taylorotwell
Copy link
Member

This is just too nasty honestly. Feel free to implement in a Gist or a package or something.

@KaneCohen
Copy link
Contributor Author

@taylorotwell, hah, yes it is nasty indeed. But, alas, some things are just nasty, especially when it comes to recursion.

I guess since #1340, is based off of that, i should close it too.

@fadillzzz
Copy link

This is something that I currently need for my project. If you don't mind, could you make a package out of it?

@KaneCohen
Copy link
Contributor Author

@fadillzzz, do you need it as part of the Core? If not, you might want to just copy/paste function into app/start/global.php and naming in something like array_select and then using that one. Problem with extending default Support package (and helpers.php file specifically) is that it's kind of a backbone of the Laravel and you can't just swap it with a fork.

@fadillzzz
Copy link

@KaneCohen Nah, not really. In fact, I've just extended the Validator class and created a pretty short validation method that solves my problem. I think I'll just roll with that instead.

@KaneCohen
Copy link
Contributor Author

@fadillzzz, added validation class with wildcard support to packagist.

@fadillzzz
Copy link

@KaneCohen heheh, didn't expect that you'd do it anyway. Thanks though, I might just fiddle around with it.

@KaneCohen
Copy link
Contributor Author

@fadillzzz, no problem. I thought of adding it anyway, just haven't had enough initiative for that.

@KaneCohen KaneCohen deleted the array_get branch February 22, 2014 20:43
gonzalom pushed a commit to Hydrane/tmp-laravel-framework that referenced this pull request Oct 12, 2023
[9.x] Default Redis cache connection is wrong
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants