How big are PHP arrays (and values) really?
nikic.github.com
How big are PHP arrays (and values) really?
1–10 of 90 posts
Re: How big are PHP arrays (and values) really?
#2Re: How big are PHP arrays (and values) really?
#3You can do things like
$arr = array(1 => 10, "1" => 11);
Or even $arr = array('他妈的我的生活' => 5);
But at the same time you can treat them as regular zero-based arrays. $arr = array();
$arr[] = 1;
$arr[] = 2;
$arr[] = 3;
$arr[] = 'dog';Re: How big are PHP arrays (and values) really?
#4As the author mentions, if you run into a use-case where you need to store 100000 integers in memory, then you should use one of the many alternative structures available. Some of them were explicitly designed to store integers in an efficient manner. Arrays weren't designed to store integers efficiently or anything else for that matter. They were designed to be fast and easy to use.
Re: How big are PHP arrays (and values) really?
#5Re: How big are PHP arrays (and values) really?
#6Re: How big are PHP arrays (and values) really?
#7PHP arrays are not really arrays, they are sort of hash-maps. You can do things like $arr = array(1 => 10, "1" => 11); Or even $arr = array('他妈的我的生活' => 5); But at the same time you can treat them as regular zero-based arrays. $arr = array(); $arr[] = 1; $arr[] = 2; $arr[] = 3; $arr[] = 'dog';
Its just the price you have to pay for not caring about the type of your "array" keys.
Re: How big are PHP arrays (and values) really?
#8PHP arrays are not really arrays, they are sort of hash-maps. You can do things like $arr = array(1 => 10, "1" => 11); Or even $arr = array('他妈的我的生活' => 5); But at the same time you can treat them as regular zero-based arrays. $arr = array(); $arr[] = 1; $arr[] = 2; $arr[] = 3; $arr[] = 'dog';
When you do an array append, the logic PHP runs is it tries to guess what the most logical key would be, add that to the hash-map and then to the end of the linked list.
$a = array();
$a[] = 'A';
$a[] = 'B';
print_r($a);
Array
(
[0] => A
[1] => B
)
$b = array();
$b[5] = 'A';
$b[100] = 'B';
$b[] = 'C';
print_r($b);
Array
(
[5] => A
[100] => B
[101] => C
)Re: How big are PHP arrays (and values) really?
#9PHP arrays are not really arrays, they are sort of hash-maps. You can do things like $arr = array(1 => 10, "1" => 11); Or even $arr = array('他妈的我的生活' => 5); But at the same time you can treat them as regular zero-based arrays. $arr = array(); $arr[] = 1; $arr[] = 2; $arr[] = 3; $arr[] = 'dog';
Is there an append operator, or something more explicit ? '+=' seems to do something strange..
Re: How big are PHP arrays (and values) really?
#10Interesting. Wonder how this compares to python and ruby memory handling.
EDIT: and for Hash with h[i] = i, it's ~6Mb