php is assoc

Solutions on MaxInterview for php is assoc by the best coders in the world

showing results for - "php is assoc"
Stefania
06 Feb 2019
1function isAssoc(array $arr)
2{
3    if (array() === $arr) return false;
4    return array_keys($arr) !== range(0, count($arr) - 1);
5}
6
7var_dump(isAssoc(['a', 'b', 'c'])); // false
8var_dump(isAssoc(["0" => 'a', "1" => 'b', "2" => 'c'])); // false
9var_dump(isAssoc(["1" => 'a', "0" => 'b', "2" => 'c'])); // true
10var_dump(isAssoc(["a" => 'a', "b" => 'b', "c" => 'c'])); // true
11
Mattia
17 May 2020
1function isAssoc(array $arr)
2{
3    if (array() === $arr) return false;
4    return array_keys($arr) !== range(0, count($arr) - 1);
5}
6
7var_dump(isAssoc(['a', 'b', 'c'])); // false
8var_dump(isAssoc(["0" => 'a', "1" => 'b', "2" => 'c'])); // false
9var_dump(isAssoc(["1" => 'a', "0" => 'b', "2" => 'c'])); // true
10var_dump(isAssoc(["a" => 'a', "b" => 'b', "c" => 'c'])); // true