What is php performance when directly accessing an array string with a specified key

I have a question about array performance .... how does PHP handle array keys? I mean, if I do something like $my_city = $cities[15]; .... does php directly access the exact string in the $cities array, or does php iterate the array until it finds a matching string?

and if it accesses the string directly ... is there a performance difference between an array with 100 rows and an array with 100,000 rows?

as in this example $my_city = $cities[15];

+4
source share
4 answers

PHP arrays are implemented as hash tables, so elements are accessible as directly as possible, without repeating everything. Read more here: http://en.wikipedia.org/wiki/Hash_table

+5
source

Access to it directly. Behind the scene are all the arithmetic of memory pointers. I can hardly believe that the concept or implementation of an array in PHP is somehow different from other languages ​​such as C, Java or C #.

0
source

It depends on what is inside the array.

 // $my_city is a reference to the variable $cities[15] = new stdobj(); $my_city = $cities[15]; // $my_city is a copy of the array row $cities[15] = 'foobar'; $my_city = $cities[15]; 

There may not be a direct answer to your question, but an object reference usually uses less memory than a variable.

0
source

there is always an obvious performance difference between an array with 100 rows and an array with 100,000 rows

-2
source

Source: https://habr.com/ru/post/1347682/


All Articles