PHP

 Home  Computers & Internet  Web Programming PHP
What is PHP?
Echo
Comments
Variables
Constants
Data Types
number_format()
Character Strings
Mathematical Operators
Comparison Operators
Logical Operators
Joining Strings
explode()
implode()
strtolower()
strtoupper()
strlen()
ucfirst()
ucwords()
strrev()
str_replace()
str_repeat()
trim()
strip_tags()
addslashes()
stripslashes()
strpos()
strrpos()
nl2br()
isset()
unset()
empty()
POST
GET
If Statements
If Else Statements
Elseif Statements
Switch Statements
For Loops
While Loops
Do While Loops
Foreach Loops
File Create
File Open
File Read
File Write
File Delete
fgets()
file_get_contents()
Date & Time
$_SERVER
Sessions
Cookies
Arrays

HOW TO MODIFY ARRAYS

Arrays, just like normal variables, can be changed at any time in your script. For example, you can add or remove elements, rearrange the order of elements or change individual values.

Example: Add Element

Suppose you have an array:

$colors=array('blue', 'red', 'green');

To add element:

$colors[ ]="black";

Now the $colors array will be:

$colors=array('blue', 'red', 'green', 'black');

Example: Remove Element

Suppose you have an array:

$colors=array('blue', 'red', 'green');

The above is equivalent to:

$colors[0]="blue";
$colors[1]="red";
$colors[2]="green";

Say, you want to remove red:

$unset($colors[1]);

Now the $colors array will be:

$colors=array('blue', 'green');

Example: Change Value

Suppose you have an array:

$colors=array('blue', 'red', 'green');

The above is equivalent to:

$colors[0]="blue";
$colors[1]="red";
$colors[2]="green";

Say, you want to change green to yellow, just do:

$colors[2]="yellow";

Now the $colors array will be:

$colors=array('blue', 'red', 'yellow');

Example: Rearrange

Suppose you have an array:

$colors=array('blue', 'red', 'green');

The above is equivalent to:

$colors[0]="blue";
$colors[1]="red";
$colors[2]="green";

Say, you want to swap blue and green around, just do:

$colors[2]="blue";
$colors[0]="green";

Now the $colors array will be:

$colors=array('green', 'red', 'blue');

See also:

What Are Arrays?
How To View Arrays
How To Create Arrays
Array arsort() Function
Array asort() Function
Array rsort() Function
Array sort() Function
Array ksort() Function
Array krsort() Function
Array Echo
Array list() Function
Manual Array Iteration
Array count() & sizeof() Function




Home | Privacy Policy | Terms Of Use | Contact Us