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
Example
As asked, I provide an example on how this could be any useful.
function is_numeric_array($array)
{
# Code below
}
function someFunction($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 ...
someFunction(array(
'PDO SQL STATEMENT',
array('param1' => 1, 'param2' => 2, 'param3' => 3),
true
));
# ... this one.
someFunction(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