0

I would like to generate a large array with a incremental key and empty value.

I tried:

$array = array();

for($i=1; $i <= 1000000; $i++)
   $array[$i] = '';

print_r($array);

thrown exception:

Fatal error:  Allowed memory size of 2097152 bytes exhausted (tried to allocate 131072 bytes)

What is the most efficient way?

3
  • 5
    Your memory is what is insufficient, not the code, but you could try array_fill(). Commented Jul 28, 2014 at 18:37
  • Do you have a php-conf.ini file used? If you want to allocate more memory than your current limit you'll have to edit some entries in that. For the record, nickb is absolutely correct. Your method of filling the array is irrelevant to the error you're getting, which is only saying that what you've made is way too big (not how you've made it). Commented Jul 28, 2014 at 18:39
  • Also, why would you want to generate a huge empty array? Commented Jul 28, 2014 at 18:41

7 Answers 7

1

To answer the title question, checkout the range function:

$a = range(0,9);
print_r($a);

Output:

Array
(
    [0] => 0
    [1] => 1
    [2] => 2
    [3] => 3
    [4] => 4
    [5] => 5
    [6] => 6
    [7] => 7
    [8] => 8
    [9] => 9
)

The issue you're having though is that you are running out of memory in php. Try increasing the memory limit if you really need an array that big.

Sign up to request clarification or add additional context in comments.

Comments

1

Use range()

$arr = range(1,1000000);

But it still won't get around the fact that your memory limit is far too low to contain the size of array you want. There is no way around this. You're trying to dump a swimming pool's worth of water into a tea cup, and wondering why it's spilling everywhere.

Comments

1

This is more memory efficient than looping:

$array = array_fill_keys(range(1,1000000), '');

Comments

1

First increase your memory size. Then the most efficient way is to use SplFixedArray

It works with PHP >= 5.3.0

$array = new SplFixedArray(1000000);

In this way you have an array with NULL values.

1 Comment

What? I'm just going by the question. Index starts at 1.
1

Much simpler and what it's for:

$array = array_fill(1, 1000000, '');

This still fails at 2MB so change memory_limit to something higher probably 64MB at least.

Comments

0

If you'd like to make more memory available to your PHP script you can increase the available memory:

http://php.net/manual/en/ini.core.php#ini.memory-limit

You can make this change in the relevant php.ini config file, or during run-time by using ini_set with the memory_limit key.

Comments

0

If you need to increase your memory for one script only, you can use ini_set();

ini_set('memory_limit','16M');

Otherwise, you can increase your memory_limit in php.ini

memory_limit = 16M

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.