- 0
Concerns about function that checks if a given element is in an array
Asked by
jaBote
-
Recently Browsing 0 members
No registered users viewing this page.
Asked by
jaBote
No registered users viewing this page.
By using this site, you agree to our Terms of Use.
Hi people.
I've recently seen other implementations of a function which just checks if a given value (numeric or string) is member on an array. It feels strange to me since I've always thought the best and most straight-forward way for doing that is via the implementation I've always done, which works for both numeric and string variables (please don't mind the .@i inicialization to 0, I know it's useless but it just does reassure me):
But I've recently seen @@Winterfox's implementation in a request's topic (this one), which is a rather creative two-line implementation (disregarding the other lines):
Which makes me wonder about what one is more efficient and scalable. I guess Winterfox's implementation is somewhat faster (an implicit loop inside of implode and a string comparison is faster than a loop in which you compare elements one by one), but I've already used mine to find the index of the array which contains a given value (change return 1; to return i; and return 0; to return -1; (for not found) along with function's description (I named it array_pos, by the way).
P.S.: I guess it'd be a good idea if we made a suggestion for getting these functions (in_array and array_pos, whichever its implementation is) inside our repository? Implemented via source or as function, but we should also get repository public functions documented since no one I know makes use of them, or even gets them rewritten to make exactly_the_same_thing.
Share this post
Link to post
Share on other sites