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
2010-07-24 12:10:16
+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
2010-07-24 12:15:01
+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
2010-07-24 12:27:12