Are numeric and associative arrays in PHP two different things?

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

提交回复
热议问题