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
Matthew Setter wrote on 08/02/2016 14:32:
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)
I like it.
I also suspect it could be implemented very efficiently in core, at
least for certain special cases, which is a good reason not to just
leave it in userland. For instance, as I understand it, some arrays in
ZE3 (PHP7) are "packed" if they consist of (mostly?) sequential
integers; any such array could instantly return true for this function.
While on the other side, a polyfill with identical semantics but worse
behaviour would be easy to provide as a composer package, reducing BC
concerns.
It would need an RFC, but that shouldn't be too hard to get drafted in
this case. https://wiki.php.net/rfc/howto
Regards,
Rowan Collins
[IMSoP]
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)
I have no strong opinion on it but I wonder if it should have a recursive
version as well, if accepted. I see it a bit like array_walk.
Cheers,
Pierre
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)
I have no strong opinion on it but I wonder if it should have a recursive
version as well, if accepted. I see it a bit like array_walk.
I did wonder about that one, especially when DB queries tend to return a
list of numeric keyed records which may be numeric or named arrays ...
--
Lester Caine - G8HFL
Contact - http://lsces.co.uk/wiki/?page=contact
L.S.Caine Electronic Services - http://lsces.co.uk
EnquirySolve - http://enquirysolve.com/
Model Engineers Digital Workshop - http://medw.co.uk
Rainbow Digital Media - http://rainbowdigitalmedia.co.uk
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)
I have no strong opinion on it but I wonder if it should have a recursive
version as well, if accepted. I see it a bit like array_walk.I did wonder about that one, especially when DB queries tend to return a
list of numeric keyed records which may be numeric or named arrays ...--
Lester Caine - G8HFLContact - http://lsces.co.uk/wiki/?page=contact
L.S.Caine Electronic Services - http://lsces.co.uk
EnquirySolve - http://enquirysolve.com/
Model Engineers Digital Workshop - http://medw.co.uk
Rainbow Digital Media - http://rainbowdigitalmedia.co.uk--
And what's wrong with doing:
https://gist.github.com/kinncj/566bbc019be5707b01f2 ?
--
--
Kinn Coelho Julião
Toronto - ON/Canada
Hi,
And what's wrong with doing:
https://gist.github.com/kinncj/566bbc019be5707b01f2 ?
Perfectly ok to me while it would be slower.
BTW, it's easy and quick to detect array is real array with PHP7.
It would be handy if kind of array can be returned. It should be
different API. is_array()
may be extended.
bool is_array(mixed $var [, bool $is_real_array]);
or
bool is_array(mixed $var [, long $type_of_array]);
where $type_of_array is bit flags such as PHP_ARRAY_REAL,
PHP_ARRAY_HASH, PHP_ARRAY, PHP_ARRAY_RECLUSIVE, etc.
Regards,
--
Yasuo Ohgaki
yohgaki@ohgaki.net
Hi,
And what's wrong with doing:
https://gist.github.com/kinncj/566bbc019be5707b01f2 ?Perfectly ok to me while it would be slower.
BTW, it's easy and quick to detect array is real array with PHP7.
It would be handy if kind of array can be returned. It should be
different API.is_array()
may be extended.bool is_array(mixed $var [, bool $is_real_array]);
or
bool is_array(mixed $var [, long $type_of_array]);
where $type_of_array is bit flags such as PHP_ARRAY_REAL,
PHP_ARRAY_HASH, PHP_ARRAY, PHP_ARRAY_RECLUSIVE, etc.
That was what I was thinking about ... It's all very well saying that
the top layer of an array is 'simple numeric', but the whole power of
PHP is that each of those elements of the array can be something
different so a simply boolean answer seems a little lacking? This looks
like a nice alternative to adding yet more functions, but an
'array_type' function returning '$type_of_array' makes a lot of sense
especially if the internal structure can maintain the information?
I may not like 'strict typing' of things, but that is because I can do
is_int and is_array to check for a single record id or a list of them,
and is_array_simple which returns true for an array which is just a
simple list of numbers or is_array_recursive for one where the list has
already been expanded to a list of objects would complete my tool kit.
--
Lester Caine - G8HFL
Contact - http://lsces.co.uk/wiki/?page=contact
L.S.Caine Electronic Services - http://lsces.co.uk
EnquirySolve - http://enquirysolve.com/
Model Engineers Digital Workshop - http://medw.co.uk
Rainbow Digital Media - http://rainbowdigitalmedia.co.uk
Yasuo Ohgaki wrote on 09/02/2016 02:52:
bool is_array(mixed $var [, long $type_of_array]);
where $type_of_array is bit flags such as PHP_ARRAY_REAL,
PHP_ARRAY_HASH, PHP_ARRAY, PHP_ARRAY_RECLUSIVE, etc.
I like this variant (although I don't like the term "real"). Unlike
returning the type, it's still amenable to optimisation, because you're
stating which checks you're interested in, so the implementation can
pick the fastest version of those checks.
Regards,
Rowan Collins
[IMSoP]
Le 08/02/2016 15:32, Matthew Setter a écrit :
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.
Interesting. If you want to check that your argument has numeric keys
only, you should change the function's name.
For a better consistency, may I suggest a function that would allow to
know whether array keys are all numeric, all strings, or mixed.
Something like array_keys_type() returning one of 3 predefined constants
: ARRAY_KEYS_NUMERIC, ARRAY_KEYS_STRING, ARRAY_KEYS_MIXED.
Regards
François
François Laupretre wrote on 08/02/2016 15:44:
For a better consistency, may I suggest a function that would allow to
know whether array keys are all numeric, all strings, or mixed.
Something like array_keys_type() returning one of 3 predefined
constants : ARRAY_KEYS_NUMERIC, ARRAY_KEYS_STRING, ARRAY_KEYS_MIXED.
The problem with this is that it's significantly harder to optimise than
cases where you are falsifying a single hypothesis:
- if the question is "are all keys integers?", you can return false as
soon as you find a string key - if the question is "are all keys strings?", you can return false as
soon as you find an integer key - if the question is "does the array contain both types of key?", you
can return false as soon as you have found one of each type of key
Combining the functions allows the 3rd optimisation only, which is
probably the least common use case. In the OP's use case, given a large
array consisting only of string keys, the function would have to examine
every key to decide whether to return ARRAY_KEYS_STRING or
ARRAY_KEYS_MIXED, although the user doesn't care about that distinction.
Regards,
Rowan Collins
[IMSoP]
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-beginnersw: 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
--
--
Kinn Coelho Julião
Toronto - ON/Canada
Kinn Julião wrote on 08/02/2016 16:05:
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.
It does the job, but it is much less efficient in some cases than the
foreach and return false case, as it must examine every element; it also
has unnecessary memory overhead, creating a filtered list which exists
only to be counted and thrown away; and an internal implementation could
be optimised even further by exploiting the way the engine stores the
array internally.
PHP is at this stage expressive enough that a large number of core
functions could be rewritten in multiple ways using other core
functions, but that doesn't automatically rule out adding more functions
to core where it is useful to do so.
Regards,
Rowan Collins
[IMSoP]
On Mon, Feb 8, 2016 at 11:38 AM, Rowan Collins rowan.collins@gmail.com
wrote:
Kinn Julião wrote on 08/02/2016 16:05:
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.It does the job, but it is much less efficient in some cases than the
foreach and return false case, as it must examine every element; it also
has unnecessary memory overhead, creating a filtered list which exists only
to be counted and thrown away; and an internal implementation could be
optimised even further by exploiting the way the engine stores the array
internally.
How does array_filter([...], 'is_int', ARRAY_FILTER_USE_KEY)
add that
much overhead?
And if that's the case, instead of introducing a 99999 array function into
the core, why not just create a new Object then?
$list = list('foo','bar'); // I know it's a reserved token, don't have time
to find a better synonym. it's just a sample
$list[2] = 'baz';
var_dump($list);
string(1) "foo"
string(1) "bar"
string(1) "baz"
$list['a'] = 'foobar'; // Fatal error.
PHP is at this stage expressive enough that a large number of core
functions could be rewritten in multiple ways using other core functions,
but that doesn't automatically rule out adding more functions to core where
it is useful to do so.
PHP is at a stage where we can add support to a better structure and
objects support... we don't need a function to check if the keys are
integer, we need an object that only supports integer as keys, or even a
generic key...
Would rather have Map<int, mixed>
than has_numeric_keys
Regards,
Rowan Collins
[IMSoP]--
--
--
Kinn Coelho Julião
Toronto - ON/Canada
On Mon, Feb 8, 2016 at 11:38 AM, Rowan Collins rowan.collins@gmail.com
wrote:Kinn Julião wrote on 08/02/2016 16:05:
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.It does the job, but it is much less efficient in some cases than the
foreach and return false case, as it must examine every element; it also
has unnecessary memory overhead, creating a filtered list which exists only
to be counted and thrown away; and an internal implementation could be
optimised even further by exploiting the way the engine stores the array
internally.How does
array_filter([...], 'is_int', ARRAY_FILTER_USE_KEY)
add that
much overhead?
And if that's the case, instead of introducing a 99999 array function into
the core, why not just create a new Object then?$list = list('foo','bar'); // I know it's a reserved token, don't have time
to find a better synonym. it's just a sample
$list[2] = 'baz';var_dump($list);
string(1) "foo"
string(1) "bar"
string(1) "baz"$list['a'] = 'foobar'; // Fatal error.
I'm confused at the goal here. Is the intent "I need to do different
logic if this is a numeric array vs. something else" or "I need to make
sure this is a numeric array, because things break otherwise"?
If the latter, $foo = array_values($bar); is a reasonably efficient and
reasonably bulletproof way to ensure you have a strictly numeric array.
PHP is at this stage expressive enough that a large number of core
functions could be rewritten in multiple ways using other core functions,
but that doesn't automatically rule out adding more functions to core where
it is useful to do so.PHP is at a stage where we can add support to a better structure and
objects support... we don't need a function to check if the keys are
integer, we need an object that only supports integer as keys, or even a
generic key...Would rather have
Map<int, mixed>
thanhas_numeric_keys
Or that. Which I like even better. Ahead-of-time typing is much safer
at scale than runtime typing. (Of course we then run smack into the
arrays-and-objects-are-just-different-enough-to-be-annoying problem that
has plagued us for a decade.)
--
--Larry Garfield
On Mon, Feb 8, 2016 at 12:53 PM, Larry Garfield larry@garfieldtech.com
wrote:
On Mon, Feb 8, 2016 at 11:38 AM, Rowan Collins rowan.collins@gmail.com
wrote:Kinn Julião wrote on 08/02/2016 16:05:
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.It does the job, but it is much less efficient in some cases than the
foreach and return false case, as it must examine every element; it also
has unnecessary memory overhead, creating a filtered list which exists
only
to be counted and thrown away; and an internal implementation could be
optimised even further by exploiting the way the engine stores the array
internally.How does
array_filter([...], 'is_int', ARRAY_FILTER_USE_KEY)
add that
much overhead?
And if that's the case, instead of introducing a 99999 array function into
the core, why not just create a new Object then?$list = list('foo','bar'); // I know it's a reserved token, don't have
time
to find a better synonym. it's just a sample
$list[2] = 'baz';var_dump($list);
string(1) "foo"
string(1) "bar"
string(1) "baz"$list['a'] = 'foobar'; // Fatal error.
I'm confused at the goal here. Is the intent "I need to do different
logic if this is a numeric array vs. something else" or "I need to make
sure this is a numeric array, because things break otherwise"?
It doesn't really matter, and the explanation is below[1] (you actually
wrote it).
If the latter, $foo = array_values($bar); is a reasonably efficient and
reasonably bulletproof way to ensure you have a strictly numeric array.Agreed.
PHP is at this stage expressive enough that a large number of core
functions could be rewritten in multiple ways using other core functions,
but that doesn't automatically rule out adding more functions to core
where
it is useful to do so.PHP is at a stage where we can add support to a better structure and
objects support... we don't need a function to check if the keys are
integer, we need an object that only supports integer as keys, or even a
generic key...Would rather have
Map<int, mixed>
thanhas_numeric_keys
Or that. Which I like even better. Ahead-of-time typing is much safer at
scale than runtime typing. (Of course we then run smack into the
arrays-and-objects-are-just-different-enough-to-be-annoying problem that
has plagued us for a decade.)[1] - That's why it doesn't matter if it's to restrict or validate...
As the top post mentions:
function isArrNum($arr) { foreach($arr as $i =>$v){ if (!is_int($i)) { return false; } } return true; }
If you want to check the keys type.
return $variable instanceof Map;
Don't want to bc?
function isArrNum($arr)
if (class_exists('Map')) $variable instanceof Map;
foreach($arr as $i =>$v){
if (!is_int($i)) {
return false;
}
}
return true;
}
Ugly? sure... but at least now we have support to a better structure...
remember: Working towards making PHP 7 even better... adding support to
Map(or whatever people wanna call it) can make it better, adding a
has_numeric_keys
, IMHO, is just an alias
to other userland options.
--
--Larry Garfield--
--
--
Kinn Coelho Julião
Toronto - ON/Canada
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-beginnersw: 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
Usually, checking only numeric keys is not enough. You may also need to
check if those numeric keys are a sequence starting from 0.
At least, that was my case when I was implementing MessagePack protocol.
And I ended up just doing this trivial check: array_values($array) ===
$array.
--
Thank you and best regards,
Eugene Leonovich
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.
array_keys($array) === range(0, count($array)-1)
This is how I check, and it seems a bit faster.
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-beginnersw: 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
Usually, checking only numeric keys is not enough. You may also need to
check if those numeric keys are a sequence starting from 0.
At least, that was my case when I was implementing MessagePack protocol.
And I ended up just doing this trivial check: array_values($array) ===
$array.
--
César D. Rodas
Open Source developer
+595-983-161124
PGP: F9ED A265 A3AB C8A1 D145 7368 158A 0336 C707 0AA6
Hi,
return array_keys($arr) !== range(0, count($arr) - 1);
array_keys($array) === range(0, count($array)-1)
That approach would fall flat when the numeric keys are not consecutive:
$array = [1=>"a", 3=>"b"];
Disclaimer: AFAIK complete goal of the function wasn't yet stated, thus
this could be considered /acceptable/.
cheers,
- Markus
Hi,
return array_keys($arr) !== range(0, count($arr) - 1);
array_keys($array) === range(0, count($array)-1)
That approach would fall flat when the numeric keys are not consecutive:$array = [1=>"a", 3=>"b"];
Disclaimer: AFAIK complete goal of the function wasn't yet stated, thus
this could be considered /acceptable/.cheers,
- Markus
I would read 'has_numeric_keys' as accepting any keys of type int or float.
Not sure if this was the intention, but would be my expectation from the
method name.
- Matt.
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.
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
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.
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,
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-beginnersw: 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
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
guilhermeblanco@gmail.com wrote on 09/02/2016 15:41:
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.
If you're going to go with hyperbole, let me put the opposite spin:
Let's improve a few details of our tried-and-tested implementation to
make everyone's life easier, rather than embarking on a 10-year mission
to radically change the entire language and deprecate every
array-related piece of PHP code ever written in favour of a
vaguely-imagined utopia whose parameters we haven't even defined yet.
I don't believe in either extreme. It's a simple proposal, which can be
examined on its own merits.
Regards,
Rowan Collins
[IMSoP]
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-beginnersw: 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
Hi,
Note that your approach ("return array_keys($arr) !== range(0,
count($arr) - 1);") does another type of check than the name
has_numeric_keys implies. Your approach will also validate that the keys
are continuous. This matches a check for non-associative arrays. Is that
the goal with this function, to help developers determine whether an
array is associative or not? Are you actually asking for a
array_is_assoc($arr) function?
// Simon