Are numeric and associative arrays in PHP two different things?

后端 未结 4 1791
庸人自扰
庸人自扰 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:11

    In general, you should read the official documentation rather than W3Schools.

    An array can contain whatever members it wants with whatever keys it wants.

    The description provided by W3Schools is quite ambiguous, or even wrong.

    • Numeric array - An array with a numeric index

    I'd say a numeric array is an array with only integer indexes. An array with one I'd probably call a mixed (or associative, see below) array, if I had to call it anything.

    • Associative array - An array where each ID key is associated with a value.

    I don't know about that description. I'd say an array can be associative if it maps strings to values instead of numerical indexes.

    • Multidimensional array - An array containing one or more arraysNumeric array - An array with a numeric index

    An associative array can contain arrays too, which makes it multidimensional.

    Keep in mind that an array with all numeric keys (even if in a string) will always be treated as a numeric array. This can mean different things in different contexts.

    $arr = array(
     '1' => 'abc',
     2 => 'def'
    );
    
    var_dump($arr);
    

    Output

    array(2) {
      [1]=>
      string(3) "abc"
      [2]=>
      string(3) "def"
    }
    
    0 讨论(0)
  • 2020-12-19 12:12

    You get an associative array. Try this code:

    $myArray[0] = 'value1';
    $myArray['one'] = 'value2';
    
    echo($myArray[1]);
    

    See? It doesn't echo anything.

    0 讨论(0)
  • 2020-12-19 12:14

    PHP doesn't really have arrays. They are dictionaries. Numeric keys are allowed at the same time as string keys. They can be mixed and do coexist.

    (Actually string keys like "123" are always treated as integers. PHP does not keep the type information for them.)

    If you want a different behaviour you could implement and extend ArrayObject however. And it would be possible to implement a map, where numeric keys functioned as alias to string indexes.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题