Voting

: max(zero, seven)?
(Example: nine)

The Note You're Voting On

alex frase
16 years ago
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).

<< Back to user notes page

To Top