1

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?

4
  • 3
    I imagine you got this code from here: stackoverflow.com/a/173479/129570. As you will see in the comments there, it's widely been pointed out that this code doesn't work correctly. Commented Apr 27, 2014 at 9:59
  • there is rarely a need to know whether an array is associated or not as regards accessing it. As every array comes with an iterator which always starts at the first record. Therefore 'current($arrayVar)' always returns the first record. Unless it has been explicitly moved. Commented Apr 27, 2014 at 10:35
  • @RyanVincent: But if you have an associative array and you know it. You'll access the array elements with key value pair. Else you would treat it as other arrays. Commented Apr 27, 2014 at 10:37
  • 1
    @Veer, i fully appreciate that. Was trying to point out that some people are not aware about the implied iterator and often use index 0 to access the first record. which can cause confusion if it has numeric indexes starting with a different value. Commented Apr 27, 2014 at 10:39

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

In the PHP manual the terms associative array and numeric array are often used to distinguish between those, e.g. "Returns an array of associative or numeric arrays holding result rows." (ref)
2

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"}

Comments

1

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.

13 Comments

Din't get you can you please provide with some code.?
@Veer: There is no code. What an associative array is or is not is not sharpely defined. Therefore there can be no code. Instead, re-read the page in the PHP manual about the array type and learn about it's specialities (that requires re-reading that page next time you're using arrays and so on and so forth. PHP is a language with many special rules, so you can't keep those all in your head. instead refer to the manual).
@Veer: Now there is some code. All arrays in PHP are associative if you ask me, each key associates to a value.
din't get you. If every array is associative, that is with key value pair, then can you please explain, how can we read an array like this: ["Veer","hakre","user12332412"] in key value type.?
@Veer: You read an array normally by traversing it via foreach. You can read each key and each value. More oldskool is the iteration via each which still works.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.