Veer Shrivastav
Veer Shrivastav

Reputation: 5496

php - Why does key of first element of an associative array cannot be zero?

I am new to associative array concept of php. I had never used associative array before this. I came through an example, and got the following code:

function isAssoc($arr)
{
    return array_keys($arr) !== range(0, count($arr) - 1);
}
echo(var_dump(isAssoc(array("0" => 'a', "1" => 'b', "2" => 'c'))).'<br />'); // false
echo(var_dump(isAssoc(array("1" => 'a', "1" => 'b', "2" => 'c'))).'<br />'); //true
echo(var_dump(isAssoc(array("1" => 'a', "0" => 'b', "2" => 'c'))).'<br />'); // true
echo(var_dump(isAssoc(array("a" => 'a', "b" => 'b', "c" => 'c'))).'<br />'); // true

The above function is used to tell whether the array is associate array or not.

I have this doubt why:

array("0" => 'a', "1" => 'b', "2" => 'c')

is not an associative array as it returns false. Whereas,

array("1" => 'a', "0" => 'b', "2" => 'c') //OR
array("1" => 'a', "1" => 'b', "2" => 'c')

is an associative array?

Upvotes: 1

Views: 1768

Answers (3)

Dejan Marjanović
Dejan Marjanović

Reputation: 19380

All arrays in PHP are associative, you can consider it to be tuple if all keys are numeric (integers but not necessarily of that type), continuous and start from 0.

Simple check would be:

function is_assoc(array $array) 
{
  $keys = array_keys($array);
  $keys_keys = array_keys($keys);

  return $keys_keys !== $keys;
}

It would yield same results as the one you've linked to/used.

A hint here would be excerpt from json_decode documentation:

assoc
When TRUE, returned objects will be converted into associative arrays.

Even if it returns "numeric" and "indexed" array it's still associative.

Another example would be:

$array = ["0" => "a", "1" => "b", "2" => "c"]; # numeric, continuous, starts from 0
json_encode($array); # (array) ["a","b","c"]

While:

$array = ["0" => "a", "2" => "b", "3" => "c"]; # numeric, NOT continuous, starts from 0
json_encode($array); # (list) {"0":"a","2":"b","3":"c"}

Upvotes: 2

salathe
salathe

Reputation: 51950

The term "associative array" in the PHP manual is used to differentiate from "indexed array". In PHP, all arrays are by definition associative in that they contain key/value pairs (the association). However, the documentation aims to refer to "associative" when it means "non-indexed", for clarity. This is the important point.

So what is an "indexed array"? By "indexed", it is meant that the keys of the array are starting at 0 and incrementing by one. Whether the keys are explicitly set at definition (array(0 => 'a', 1 => 'b')) or implicit (array('a', 'b')) makes no difference. Any other sequence of keys would be referred to as "associative".

Note that how the PHP manual uses the term "associative" doesn't necessarily correlate precisely with the same term's use elsewhere.

Upvotes: 2

hakre
hakre

Reputation: 198126

The function you're referring to has flaws and it is not authoritative about the fact whether or not an array in PHP is associative or not.

In-fact, in PHP it is not strictly defined what the term associative array actually means.

However it is important to understand that

  1. PHP is loosely typed
  2. PHP differs in array keys between integer (bool, NULL, float, integer, numeric string represented as integer) and string (nun-numeric strings) for keys.

Most likely an associative array in PHP is one where inside the array (that is after creating it, not while it seems when writing the array definition) that all keys in that array are strings.

But keep in mind that no set-in-stone definition exists. PHP arrays are just a mixture of array and hash-map where you need to know both without having both properly differentiated and the need to keep in mind the difference between numeric and non-numeric keys and how the loosely type-system of PHP converts for the array key needs.

First page to go as usual:


For those who really can only understand it with code, here the pony goes:

function is_array_assoc(array $array) {
    return is_array($array);
}

If you can ensure that you're not using an error-handler that catches catchable errors, then the following code is correct, too:

function is_array_assoc(array $array) {
    return TRUE;
}

The later example makes it perhaps a bit more clear that all arrays in PHP are associative.

Upvotes: 1

Related Questions