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.
--
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>*