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?
2 Answers
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';
1 Comment
PapaHotelPapa
I disagree. Prefilling an array with empty members can avoid later having to check whether the index you want to access has been defined. I'm currently using a technique to pass an array that applies classes to twitter bootstrap elements so that, in my case, my tab pane of choice is already open when I load the page after a redirect. I could have achieved a similar effect with JS, I know, and that would have been fine. My only point is that there are cases when this kind of code is desirable.
You could use array_fill() for that:
$array = array_fill(1, 50, ""); // creates [1]...[50] with "" string
2 Comments
Your Common Sense
He could but he shouldn't. He's also wants to fill it with empty arrays
Camilla Horne
Multidimentional array: array_fill(1, 50, array_fill(1, 50, ""));