Re: Proposal for a new array function
| From: | guilhermeblanco@gmail.com | Date: | Tue, 09 Feb 2016 15:41:25 +0000 |
| Subject: | Re: Proposal for a new array function | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-91165@lists.php.net to get a copy of this message | ||
My personal take on this:
Let's add just more 1 function over a 99999 function's array API, because I
want to optimize 3 lines in my PHP code, and language lack of Generics
while we still refuse to carefully think about a proper OO Collection API.
Regards,
On Tue, Feb 9, 2016 at 6:19 AM, Matthew Setter <matthew@maltblue.com> wrote:
> Thanks kindly for the rapid and very helpful feedback. I'm going over it
> today and will collate it and think over it further. Given that the
> feedback's been so constructive and positive, I'll be getting started on an
> RFC over the next day or so.
>
> Matt
>
>
> --
> Kind regards,
>
>
> *Matthew Setter*
> *Freelance Software Developer & Technical Writer *
> *Host of Free the Geek <http://freethegeek.fm> | Author
> of Zend Framework
> 2
> Foundations
> <https://leanpub.com/zendframework2-for-beginners>*
>
> w: http://www.matthewsetter.com
> t: *@settermjd <https://www.twitter.com/settermjd>*
> g+: *+MatthewSetterFreelanceWriterDeveloper
>
> <https://plus.google.com/u/0/+MatthewSetterFreelanceWriterDeveloper/posts
> >*
> li: *in/matthewsetter
> <https://www.linkedin.com/in/matthewsetter>*
>
> On Mon, Feb 8, 2016 at 6:43 PM, Matt Prelude <me@mprelu.de> wrote:
>
> > Hi,
> >
> >> I want to propose a new PHP array method, called has_numeric_keys (or
> >> something similar/better), that would have the following method
> signature:
> >>
> >> bool has_numeric_keys(array $array)
> >>
> >> The reason for it is to check if the array passed to it only had numeric
> >> keys.
> >>
> >> Why this method, when you could do it in userland PHP? Answer?
> >> Convenience.
> >> I found, recently, that I had to perform this kind of check, when
> patching
> >> Zend\Db\Sql\Insert.php. The approach I took was this:
> >>
> >> return array_keys($arr) !== range(0, count($arr) - 1);
> >>
> >> Not sure of my approach, I took to Twitter and received the following
> >> suggestions, amongst others:
> >>
> >> function isArrNum($arr) {
> >> foreach($arr as $i =>$v){
> >> if (!is_int($i)) {
> >> return false;
> >> }
> >> }
> >> return true;
> >> }
> >>
> >> count(array_filter(array_keys($array), 'is_string')) > 0
> >>
> >> array_filter([...], 'is_int', ARRAY_FILTER_USE_KEY)
> >>
> >> This convinced me that it wasn't just me seeing a valid use case for it,
> >> and that others have implemented differing solutions when presented with
> >> the same situation. Given that, I believe a simple, utility, function
> such
> >> as this would be of help.
> >>
> >> As for who would implement it, that would be me.
> >>
> > I like it. Would like to see it be a bit more generic though, something
> > like (ignore the name, I can't name functions):
> >
> > array_validate_keys($array, is_int);
> >
> > I say this because it allows for more potential uses (that and I've done
> > exactly the same to check all keys are strings before).
> >
> > - Matt.
> >
> >
>
--
Guilherme Blanco
MSN: guilhermeblanco@hotmail.com
GTalk: guilhermeblanco
Toronto - ON/Canada