Are numeric and associative arrays in PHP two different things?

后端 未结 4 1792
庸人自扰
庸人自扰 2020-12-19 12:03

This is a deeper dive into a previous question I had here: Can items in PHP associative arrays not be accessed numerically (i.e. by index)?

According to W3Schools, :

4条回答
  •  感情败类
    2020-12-19 12:28

    All arrays in PHP are the same; they're implemented as hash maps which associate keys to values, whatever type the keys may be.

    Manual:

    The indexed and associative array types are the same type in PHP, which can both contain integer and string indices.

    If an array had both numeric and non-numeric indices, though, I'd still call it an associative array. The meaning of "associative" still stands.

    Wikipedia:

    An associative array is an abstract data type composed of a collection of unique keys and a collection of values, where each key is associated with one value (or set of values).

    ...

    From the perspective of a computer programmer, an associative array can be viewed as a generalization of an array. While a regular array maps an integer key (index) to a value of arbitrary data type, an associative array's keys can also be arbitrarily typed. In some programming languages, such as Python, the keys of an associative array do not even need to be of the same type.

    For the last sentence, the same applies for PHP, as shown in your example.

提交回复
热议问题