N
N
Nikolino2018-09-07 21:58:30
Arrays
Nikolino, 2018-09-07 21:58:30

What is required for an array to be considered associative?

Wikipedia says that an associative array is one that has a key and a value.
In both PHP and JavaScript, each element in an array has a key. Does this mean that in both PHP and JavaScript all arrays are associative?
In Koterov's book on PHP, the author writes that "strictly speaking", all arrays in PHP are associative, because all elements have a key.
In JavaScript, too, all array elements have a key, but for some reason they don’t call it associative, perhaps because only numbers can be used as an index.
And if in JS you want to use strings in the keys, then you need to use objects.
I am not strong in other languages, and there is no corresponding education.
Tell me, are there PLs in which array elements may not have a key? If so, then what is the point of them if we cannot get a certain value (there is no key)?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dollar, 2018-09-07
@Nikolino

There are languages ​​in which a function reference can be used as a key.
The key can also be a reference to an object.
Moreover, you can make a key-value pair, where the key and value will point to the same object.
Sometimes this is helpful. But in general - a perversion. Although if the object points to itself, then it is already difficult to say that there is a key.
You correctly noted everything. If the keys are numbers, that is, indices, then the array is indexed. It is not customary to call such an array as associative, although formally it is possible to work as with an associative one (depending on the PL). Here, however, there is a subtlety - the indices may be out of order, but these are details.
In general, it will not work without a key, because. the values ​​have to be stored somewhere, otherwise they simply won't be part of the array, but will be individual values ​​or expressions on their own. An array combines them and uses keys (or indices) as addressing.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question