solvit.io

Check if a PHP array is associative or scalar (only contains numeric keys)

Solution
function isAssociative($array) {
    return count(array_filter(array_keys($array), 'is_string')) > 0;
}
Example
$result = isAssociative([1 => 'a', 2 => 'b', 3 => 'c']);
// $result = false

$result = isAssociative(['a' => 1, 'b' => 2, 'c' => 3]);
// $result = true
Find more solutions about: ,