I found that in PHP (or i probably can't find it) a proper is_numeric_array($array)
function is missing. So i created one. The problem is that i don't think it's great and i don't know how to improve it.
Any suggestion?
My first function
function is_numeric_array($array)
{
$i = 0;
foreach ($array as $a => $b) { if (is_int($a)) { ++$i; } }
if (count($array) === $i) { return true; }
else { return false; }
}
is_numeric_array(array(0,0,0,0,0)); // true
is_numeric_array(array('str' => 1, 'str2' => 2, 'str3' => 3)); // false
New hero function
function is_numeric_array($array) {
foreach ($array as $a=>$b) {
if (!is_int($a)) {
return false;
}
}
return true;
}
Thanks to sepp2k and Michael
Another way of doing it
if (isset($array[1])) { // = is_array_function($array) }
if (isset($array['key'])) { // = !is_array_function($array) }
Thanks to edorian
Example
As asked, i provide an example on how this could be any usefull.
# Our hero function
function is_numeric_array($array)
{
# Code below
}
# This function get me horny, like ponies does
function hornyFunction($array)
{
if (is_numeric_array($array))
{
$query = $array[0];
$param = $array[1];
$fetch = $array[2];
}
else
{
$query = $array['query'];
$param = $array['param'];
$fetch = $array['fetch'];
}
# Do your sql/pdo stuff here
}
# This use is the same of ...
hornyFunction(array(
'PDO SQL STATEMENT',
array('param1' => 1, 'param2' => 2, 'param3' => 3),
true
));
# ... this one.
hornyFunction(array(
'query' => 'PDO SQL STATEMENT',
'param' => array('param1' => 1, 'param2' => 2, 'param3' => 3),
'fetch' => true
));
# To choose one form instead of the other is coder's decision
# Also i know it is useless but i was just wondering why anybody actually looked forward this function