Answers for "check array is associative php"

PHP
1

php check array is not associative

count(array_filter(array_keys($array), 'is_string')) === 0
Posted by: Guest on September-03-2020
1

php array is associative

function isAssoc(array $arr)
{
    if (array() === $arr) return false;
    return array_keys($arr) !== range(0, count($arr) - 1);
}

var_dump(isAssoc(['a', 'b', 'c'])); // false
var_dump(isAssoc(["0" => 'a', "1" => 'b', "2" => 'c'])); // false
var_dump(isAssoc(["1" => 'a', "0" => 'b', "2" => 'c'])); // true
var_dump(isAssoc(["a" => 'a', "b" => 'b', "c" => 'c'])); // true
Posted by: Guest on August-26-2021

Code answers related to "check array is associative php"

Browse Popular Code Answers by Language