最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Does the internal index of a PHP array automatically increase, if I create an array element with []? - Stack Overflow

programmeradmin3浏览0评论

I need to understand through the PHP official documentation if in this case:

$vett[]=8.0;
$vett[]=6.5;

the internal index is increased automatically (to point to the next free location) soon after the end of the first statement or at the beginning of the second statement.


The confusion arises from the book of Robert Nixon:

I need to understand through the PHP official documentation if in this case:

$vett[]=8.0;
$vett[]=6.5;

the internal index is increased automatically (to point to the next free location) soon after the end of the first statement or at the beginning of the second statement.


The confusion arises from the book of Robert Nixon:

Share Improve this question edited 6 hours ago Federica Guidotti asked 8 hours ago Federica GuidottiFederica Guidotti 1711 silver badge11 bronze badges 4
  • 2 Just be advised, that this book is truly horrible. It is not just this part is wrong. The book was written at the time of PHP4 by someone whose PHP knowledge was very low. It underwent some editions, but remained essentially the same at the core, with some new blunders introduced. If you can, try PHP&MySQL by Jon Duckett, which is much more modern and contain less mistakes. – Your Common Sense Commented 5 hours ago
  • 1 I suggest "Modern PHP: New Features and Good Practices" by Josh Lockhart, more modern and with useful insight to write good PHP code. – monblu Commented 5 hours ago
  • Why not try it out and see what happens? It's possible to dump the used array keys at any time – Nico Haase Commented 4 hours ago
  • How does PHP index associative arrays? – mickmackusa Commented 45 mins ago
Add a comment  | 

1 Answer 1

Reset to default 2

First of all, internal array pointer means something absolutely different. It's a thing related to some outdated method for iterating over existing arrays (which is almost never used nowadays).

What you are talking about could be called "internal index", but there is no such thing in PHP.

The index is just set automatically at the beginning of each statement.

The PHP manual explains it pretty straightforward:

if no key is specified, the maximum of the existing int indices is taken, and the new key will be that maximum value plus 1 (but at least 0). If no int indices exist yet, the key will be 0 (zero).

So,

  • when you do $vett[]=8.0;, if $vett doesn't exist or doesn't have numerical indices yet, the key is set to 0, so its the same as doing $vett[0]=8.0;
  • when you do $vett[]=6.5;, PHP gets the maximum numeric key (which is 0), adds 1 and then the key is set 1, so its the same as doing $vett[1]=8.0;

everything is done right on the assignment, there is no "internal index" to be kept. Just a maximum index from the array. Check this code:

$vett = [];
$vett[60]=8.0;
$vett[]=6.5;
var_dump($vett);

It's as simple as max index+1 right on the assignment.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论