There are basically 3 ways for adding new elements into an array in php.
1. array_push
Push one or more elements onto the end of array
2. array_unshift
Prepend one or more elements to the beginning of an array
3. Using the $array[] = $var notation. For example, in a for loop, you can keep on adding elements ($var) into $array as long as the for condition is met.
$count = array();
for ($i=0; $i<=6; $i++) {
$count[] = $i;
}
print_r($count); The result you'll get back is:
Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 [6] => 6 )
Leave a Reply