How To Add Element To An Array In Php
Posted On Thu Feb 17, 2011 By hostman In PHP Forums And Topics Discussions About Hyper Text Programming Language Forums
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:
the output would look something like this for the $colors array:
OUTPUT:
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:
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:
so now, the array contains all these values:
OUTPUT:
hope that helps
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
)
(
[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
)
(
[0] => blue
[1] => red
[2] => yellow
[3] => green
[4] => black
[5] => white
)
hope that helps
Related Content
Information
Forums »
PHP Forums And Topics Discussions About Hyper Text Programming Language »
How To Add Element To An Array In Php
PHP Forums And Topics Discussions About Hyper Text Programming Language »
How To Add Element To An Array In Php
Title: How To Add Element To An Array In Php
Description: 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
Tags: php , array , values , elements , code , script , add , put , insert , increment
Info: This Post Has Been Viewed 0 Times Since
Date: Thu Feb 17, 2011
Author hostman Received 0 Replies #1293
Date: Thu Feb 17, 2011
Author hostman Received 0 Replies #1293
Share
URL: 

Embed: 

To embed this topic, just copy the code from the "Embed" box. Once you've copied the code, just paste it into your website or blog to embed it
BBCODE:: 

BBCODE is use on forums. You can put this code on all your BBCODE enabled forums like PhpBB, vBulletin® and others. Just Copy and Paste this code on your Posts and Replies on your forums
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
copyright © 2013 wallpaperama - All Rights Reserved - Last Updated Mon May 06, 2013 (-8 GMT)
Powered by: Webune Forums V5