nkcmr
nkcmr

Reputation: 11010

PHP declaring empty arrays?

I have to make a two-dimensional array based on the number of lets say car parts that is submitted on a form. Is it possible to declare an array of a certain size and THEN go back and fill it?

Upvotes: 4

Views: 15663

Answers (2)

mario
mario

Reputation: 145492

You could use array_fill() for that:

$array = array_fill(1, 50, "");   // creates [1]...[50] with "" string

Upvotes: 3

mellamokb
mellamokb

Reputation: 56779

PHP arrays are dynamically allocated. There's no reason to create it ahead of time. Just start using the indexes as you need them:

$myNewVar[0] = 'data';

Upvotes: 10

Related Questions