Php create empty array of size

I am creating a new array in a for loop.

for $i < $number_of_items
    $data[$i] = $some_data;

PHP keeps complaining about the offset since for each iteration I add a new index for the array, which is kind of stupid.

Notice: Undefined offset: 1 in include() (line 23 of /...
Notice: Undefined offset: 1 in include() (line 23 of /..
Notice: Undefined offset: 1 in include() (line 23 of /..

Is there some way to predefine the number items in the array so that PHP will not show this notice?

In other words, can I predefine the size of the array in a similar way to this?

$myarray = array($size_of_the_earray);

Php create empty array of size

asked Mar 22, 2011 at 0:39

Reed RichardsReed Richards

3,9788 gold badges38 silver badges54 bronze badges

2

There is no way to create an array of a predefined size without also supplying values for the elements of that array.


The best way to initialize an array like that is array_fill. By far preferable over the various loop-and-insert solutions.

$my_array = array_fill(0, $size_of_the_array, $some_data);

Every position in the $my_array will contain $some_data.

The first zero in array_fill just indicates the index from where the array needs to be filled with the value.

Php create empty array of size

answered Mar 22, 2011 at 0:42

Php create empty array of size

9

Potentially relevant- if you want to initialize and fill an array with a range of values, use PHP's (wait for it...) range function:

$a = range(1, 5);  // array(1,2,3,4,5)
$a = range(0, 10, 2); // array(0,2,4,6,8,10)

answered Dec 4, 2013 at 1:24

MadbreaksMadbreaks

18.4k7 gold badges55 silver badges70 bronze badges

You can't predefine a size of an array in php. A good way to acheive your goal is the following:

// Create a new array.
$array = array(); 

// Add an item while $i < yourWantedItemQuantity
for ($i = 0; $i < $number_of_items; $i++)
{
    array_push($array, $some_data);
    //or $array[] = $some_data; for single items.
}

Note that it is way faster to use array_fill() to fill an Array :

$array = array_fill(0,$number_of_items, $some_data);

If you want to verify if a value has been set at an index, you should use the following: array_key_exists("key", $array) or isset($array["key"])

See array_key_exists , isset and array_fill

answered Mar 22, 2011 at 0:42

1

PHP Arrays don't need to be declared with a size.

An array in PHP is actually an ordered map

You also shouldn't get a warning/notice using code like the example you have shown. The common Notice people get is "Undefined offset" when reading from an array.

A way to counter this is to check with isset or array_key_exists, or to use a function such as:

function isset_or($array, $key, $default = NULL) {
    return isset($array[$key]) ? $array[$key] : $default;
}

So that you can avoid the repeated code.

Note: isset returns false if the element in the array is NULL, but has a performance gain over array_key_exists.

If you want to specify an array with a size for performance reasons, look at:

SplFixedArray in the Standard PHP Library.

answered Mar 22, 2011 at 0:45

JacobJacob

8,1831 gold badge22 silver badges29 bronze badges

3

There is also array_pad. You can use it like this:

$data = array_pad($data,$number_of_items,0);

For initializing with zeros the $number_of_items positions of the array $data.

localheinz

8,6172 gold badges31 silver badges42 bronze badges

answered Jun 23, 2014 at 18:51

Php create empty array of size

AcademiaAcademia

3,8246 gold badges30 silver badges49 bronze badges

PHP provides two types of array.

  • normal array
  • SplFixedArray

normal array : This array is dynamic.

SplFixedArray : this is a standard php library which provides the ability to create array of fix size.

answered Jan 7, 2015 at 18:56

How do I create an empty array in PHP?

Syntax to create an empty array: $emptyArray = []; $emptyArray = array(); $emptyArray = (array) null; While push an element to the array it can use $emptyArray[] = “first”. At this time, $emptyArray contains “first”, with this command and sending “first” to the array which is declared empty at starting.

How do you create an empty array?

Substituting with a new array − arr = []; This is the fastest way. ... .
Setting length prop to 0 − arr.length = 0. This will clear the existing array by setting its length to 0. ... .
Splice the whole array. arr.splice(0, arr.length) This will remove all elements from the array and will actually clean the original array..

Are PHP arrays fixed size?

Arrays don't have a set size, they are dynamic (not stored the same as other languages such as C). If you want to specify an array with a size for performance reasons, look at: SplFixedArray in the Standard PHP Library.

Is empty array in PHP?

php $emptyArray = array(); $isEmpty = empty($emptyArray); echo("The function has returned $isEmpty. ... Use empty() Function to Check Whether an Array Is Empty in PHP..