- Forums
- PHP
- How To Add Additonal Elements To An Existing Array In Php
this post will show you how to add element to an array in php - just follow this easy instructions i wil give you so you can learn how to put additional values or string to increment a string value in an array [1293], Last Updated: Sat May 18, 2024
wallpaperama
Thu Feb 17, 2011
0 Comments
851 Visits
hello
if you are reading my post here is probably because you want to know how you can add an additional element to your array in php.
for example, lets say i have an array that looks like this:
PHP CODE:
$colors = array('blue','red','yellow');
the output would look something like this for the $colors array:
OUTPUT:
Array
(
[0] => blue
[1] => red
[2] => yellow
)
now, lets say that later in the script you want to add another element, you want to add green to the array, its very easy, just use the
push() function in PHP like this:
PHP CODE:
array_push($colors, 'green');
how about if you wanted to add more than one element, here is an example on how you can add multiple elements at once, here i am adding two more:
PHP CODE:
array_push($colors, 'black','white');
so now, the array contains all these values:
OUTPUT:
Array
(
[0] => blue
[1] => red
[2] => yellow
[3] => green
[4] => black
[5] => white
)
hope that helps