tags:

views:

83

answers:

4

How would I create an array in PHP that has $x empty elements? The $x is unknown and varying value. For example, if I wanted to create an array of 3 elements, I could just do:

$array = array(null,null,null);

However, I don't know what $x is and there could be million elements, I need to do this automatically.

+8  A: 

As usual with PHP there's a function for this: array_fill()

preinheimer
+1  A: 

You can do it like this:

array_fill(0, $x, 'value')
Hanse
A: 

How about range(), array_pad() or array_fill() or SplFixedArray

Gordon
range() is not acceptable in his scenarion as he needs the values to be null (though it boggles my mind why one would need that ...)
Jan Hančič
+3  A: 

In PHP arrays are a dynamic datatype that you can add items to on the fly as well as delete some. So you don’t need to allocate the array upfront.

Gumbo
Didn't answer my question..
rFactor
@Kaitsuli: Then why would you want to create an array with million *null* values?
Gumbo
I'm using .NET random generator that fills up my array. It will fill all existing array elements that are registered.
rFactor