Please note that the 'cast to array' check is horrendously out of date.
Running that code against PHP 5.6 results in this:
is_array : 0.93975400924683
cast, === : 1.2425191402435
So, please use 'is_array', not the horrible casting hack.(PHP 4, PHP 5, PHP 7, PHP 8)
is_array — Verifica se a variável é um array
valueA variável a ser avaliada.
Exemplo #1 Verificando se a variável é um array
<?php
$sim = array('este', 'é', 'um array');
echo is_array($sim) ? 'Array' : 'não Array';
echo "\n";
$nao = 'esta é uma string';
echo is_array($nao) ? 'Array' : 'não Array';
?>O exemplo acima produzirá:
Array não Array
Please note that the 'cast to array' check is horrendously out of date.
Running that code against PHP 5.6 results in this:
is_array : 0.93975400924683
cast, === : 1.2425191402435
So, please use 'is_array', not the horrible casting hack.Or you could make use of the array_diff_key and array_key function:
<?php
function is_assoc($var)
{
return is_array($var) && array_diff_key($var,array_keys(array_keys($var)));
}
function test($var)
{
echo is_assoc($var) ? "I'm an assoc array.\n" : "I'm not an assoc array.\n";
}
// an assoc array
$a = array("a"=>"aaa","b"=>1,"c"=>true);
test($a);
// an array
$b = array_values($a);
test($b);
// an object
$c = (object)$a;
test($c);
// other types
test($a->a);
test($a->b);
test($a->c);
?>
The above code outputs:
I'm an assoc array.
I'm not an assoc array.
I'm not an assoc array.
I'm not an assoc array.
I'm not an assoc array.
I'm not an assoc array.hperrin's results have indeed changed in PHP 7. The opposite is now true, is_array is faster than comparison:
is_array : 0.52148389816284
cast, === : 0.84179711341858
Tested 1000000 iterations.Yet another simpler, faster is_assoc():
<?php
function is_assoc($array) {
foreach (array_keys($array) as $k => $v) {
if ($k !== $v)
return true;
}
return false;
}
?>
In my tests it runs about twice as fast as Michael/Gabriel's array_reduce() method.
(Speaking of which: Gabriel's version doesn't work as written; it reports associative arrays as numeric if only the first key is non-numeric, or if the keys are numeric but ordered backwards. Michael solves this problem by comparing array_reduce() to count(), but that costs another function call; it also works to just compare to -1 instead of 0, and therefore return -1 as the ternary else from the callback).alex frase's example is fast but elanthis at awesomeplay dot com's example is faster and Ilgar's modification of alex's code is faulty (the part " || $_array[$k] !== $v"). Also, Ilgar's suggestion of giving a false return value when the variable isnt an array is not suitable in my opinion and i think checking if the array is empty would also be a suitable check before the rest of the code runs.
So here's the modified (is_vector) version
<?php
function is_vector( &$array ) {
if ( !is_array($array) || empty($array) ) {
return -1;
}
$next = 0;
foreach ( $array as $k => $v ) {
if ( $k !== $next ) return true;
$next++;
}
return false;
}
?>
and the modified (alex's is_assoc) version
<?php
function is_assoc($_array) {
if ( !is_array($_array) || empty($array) ) {
return -1;
}
foreach (array_keys($_array) as $k => $v) {
if ($k !== $v) {
return true;
}
}
return false;
}
?>I've found a faster way of determining an array. If you use is_array() millions of times, you will notice a *huge* difference. On my machine, this method takes about 1/4 the time of using is_array().
Cast the value to an array, then check (using ===) if it is identical to the original.
<?php
if ( (array) $unknown !== $unknown ) {
echo '$unknown is not an array';
} else {
echo '$unknown is an array';
}
?>
You can use this script to test the speed of both methods.
<pre>
What's faster for determining arrays?
<?php
$count = 1000000;
$test = array('im', 'an', 'array');
$test2 = 'im not an array';
$test3 = (object) array('im' => 'not', 'going' => 'to be', 'an' => 'array');
$test4 = 42;
// Set this now so the first for loop doesn't do the extra work.
$i = $start_time = $end_time = 0;
$start_time = microtime(true);
for ($i = 0; $i < $count; $i++) {
if (!is_array($test) || is_array($test2) || is_array($test3) || is_array($test4)) {
echo 'error';
break;
}
}
$end_time = microtime(true);
echo 'is_array : '