tags:

views:

50

answers:

4

I want to add data to an array dynamically.

+1  A: 
$array[] = 'Hi';

pushes on top of the array.

$array['Hi'] = 'FooBar';

sets a specific index.

nikic
+1  A: 

Like this?:

$array[] = 'newItem';
Mewp
+1  A: 

In additon to directly accessing the array, there is also

array_push — Push one or more elements onto the end of array

Gordon
+2  A: 

There are quite a few ways to work with dynamic arrays in PHP. Initialise an array:

$array = array();

Add to an array:

$array[] = "item";
$array[$key] = "item";
array_push($array, "item", "another item");

Remove from an array:

$item = array_pop($array);
$item = array_shift($array);
unset($array[$key]);

There are plenty more ways, these are just some examples.

SickAnimations