Download - PHP Arrays
![Page 1: PHP Arrays](https://reader034.vdocument.in/reader034/viewer/2022052307/554f9369b4c905435d8b51cf/html5/thumbnails/1.jpg)
PHP Arrays.The Good, the Bad and the Ugly.
Mahesh Gattani
![Page 2: PHP Arrays](https://reader034.vdocument.in/reader034/viewer/2022052307/554f9369b4c905435d8b51cf/html5/thumbnails/2.jpg)
About Me
● Senior Software Engineer at Lucid Software Inc.● Bachelors degree from IIT Delhi, India in Computer Science.● Interests:
○ Sports, not the adventure types. Cricket anyone?○ Long Drives○ Reading
● Follow me on○ methinking.tumblr.com
![Page 3: PHP Arrays](https://reader034.vdocument.in/reader034/viewer/2022052307/554f9369b4c905435d8b51cf/html5/thumbnails/3.jpg)
What are arrays?
![Page 4: PHP Arrays](https://reader034.vdocument.in/reader034/viewer/2022052307/554f9369b4c905435d8b51cf/html5/thumbnails/4.jpg)
PHP arrays: The good.
● Exceptionally easy to use.● No need to allocate memory.● Indexed and associative at the same time.● Different types of keys allows in the same instance.● Ordered.● ...
PHP arrays are not arrays...They are... Ordered Hash Maps
![Page 5: PHP Arrays](https://reader034.vdocument.in/reader034/viewer/2022052307/554f9369b4c905435d8b51cf/html5/thumbnails/5.jpg)
PHP arrays: The bad
PHP arrays are bad because…
● Runtime○ O(n*n) worst case
■ Why you ask? Because they are maps. More about this later.○ Rehashing.
● Memory Usage○ Whole different beast.
![Page 6: PHP Arrays](https://reader034.vdocument.in/reader034/viewer/2022052307/554f9369b4c905435d8b51cf/html5/thumbnails/6.jpg)
UGLY!
Weird, huh!?
Size: 8?Next Free: 6?
Bucket2, Bucket4?● Collisions!
![Page 7: PHP Arrays](https://reader034.vdocument.in/reader034/viewer/2022052307/554f9369b4c905435d8b51cf/html5/thumbnails/7.jpg)
Hashmaps and collisions
PHP hash function● Integers: Integer mod size● Strings: Hash function DJBX33A
PHP DOS Attack by super colliding arrays!
Fixed in 5.3.9
![Page 8: PHP Arrays](https://reader034.vdocument.in/reader034/viewer/2022052307/554f9369b4c905435d8b51cf/html5/thumbnails/8.jpg)
What to do?
Find your use case.
Ex: Judy array
![Page 9: PHP Arrays](https://reader034.vdocument.in/reader034/viewer/2022052307/554f9369b4c905435d8b51cf/html5/thumbnails/9.jpg)
PHP Interfaces
● Array Access○ Interface to provide accessing objects as arrays
● Iterator○ Interface for external iterators or objects that can be iterated
themselves internally.
● Countable○ Classes implementing this can be used with count() function.
![Page 10: PHP Arrays](https://reader034.vdocument.in/reader034/viewer/2022052307/554f9369b4c905435d8b51cf/html5/thumbnails/10.jpg)
Example
SQLiteDBArray
A simple, in memory, indexed array implementation in PHP using SQLiteDB
https://github.com/maheshgattani/SQLiteDBArray
![Page 11: PHP Arrays](https://reader034.vdocument.in/reader034/viewer/2022052307/554f9369b4c905435d8b51cf/html5/thumbnails/11.jpg)
References
● https://sheriframadan.com/2012/10/a-closer-look-into-php-arrays/● http://nikic.github.io/2011/12/28/Supercolliding-a-PHP-array.html● www.php.net● https://github.com/maheshgattani
![Page 12: PHP Arrays](https://reader034.vdocument.in/reader034/viewer/2022052307/554f9369b4c905435d8b51cf/html5/thumbnails/12.jpg)
Thank you!
![Page 13: PHP Arrays](https://reader034.vdocument.in/reader034/viewer/2022052307/554f9369b4c905435d8b51cf/html5/thumbnails/13.jpg)
Lucidchart● Building the next generation of
collaborative web applications.● VC funded, high growth,
profitable.● Graduates from Harvard, MIT,
Stanford● Team has worked at Google,
Amazon, Microsoft.
https://www.lucidchart.com/jobs