Wallpapers Forums Login

How To Add Element To An Array In Php

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

Related Content

Information

Share

wallpaperama | Wallpapers | Forums | Terms Of Service

copyright © 2013 wallpaperama - All Rights Reserved - Last Updated Mon May 06, 2013 (-8 GMT)
Powered by: Webune Forums V5