Re: Proposal for a new array function

From: Date: Mon, 08 Feb 2016 16:05:05 +0000
Subject: Re: Proposal for a new array function
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-91120@lists.php.net to get a copy of this message
On Mon, Feb 8, 2016 at 9:32 AM, Matthew Setter <matthew@maltblue.com> wrote: > 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. > That's the nice thing about userland... you are giving them ability to implement what they want! array_filter([...], 'is_int', ARRAY_FILTER_USE_KEY) does the job as the others... don't think having a new function would prevent this. > > As for who would implement it, that would be me. > > -- > 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 > <Á¹³ > bv „½sÁÔ¢g‘https://plus.google.com/u/0/+MatthewSetterFreelanceWriterDeveloper/posts > >* > li: *in/matthewsetter > <https://www.linkedin.com/in/matthewsetter>* > -- *--* *Kinn Coelho Julião* *Toronto - ON/Canada*

« previous php.internals (#91120) next »