Page 1 of 1

create array

Posted: Thu Dec 06, 2007 2:50 am
by tommy1987
How do I create an array of a set amount of elements in PHP.
All I can find is examples of where an array is created and the elements are immediately set.
What I want is something like

Code: Select all

$new_arr = new Array(1000);
i.e. to create an element of 1000 elements, must we always specify this when creating an array or can you have a totally dynamic array?

Thanks.

Posted: Thu Dec 06, 2007 3:04 am
by Scrumpy.Gums
You can create a dynamic array with:

Code: Select all

$new_arr = array();
No need to set the size as you do in Java and other languages.

If you want to set initial values for a 1000 elements you could use array_fill()

http://uk.php.net/manual/en/function.array-fill.php


Hope that helps

Posted: Thu Dec 06, 2007 4:40 am
by php_daemon

Code: Select all

$myarray = range(0, 999);