Viper Creations
Comeback Contest
Check out our forums for the an explosive comeback contest. We are giving away everything from iPods to Visual Studio. more...
»User: »Password:   Remember Me? 
Webmaster Forum - Viper Creations / Programming / PHP/MySQL / An Introduction to PHP Arrays
Posted:  08 Nov 2006 23:12
Arrays are a complex element of the language to grasp but unfortunately these things are not going to go away as they are extremely useful for doing many processes programmatically.

So lets jump in at the deep end with creating an array.

Creating an array
To initialize an array simply use the following code
PHP Code:
Code:


<?
$arrayVar = array();
?>

This does nothing special, it just creates a new array named $arrayVar.

Lets add some values to this array.
PHP Code:
Code:


<?
$arrayVar = array('foo','bar','baz');
?>

The above code changes the empty array into a 3 part array with 3 variables: foo, bar and baz. You can now access these variables with their indexes 0,1 and 2

for example:
PHP Code:
Code:


<?
$arrayVar = array('foo','bar','baz'); //creates the array

print $arrayVar[0]; // this would print the word 'foo' to the screen

print $arrayVar[1]; //this would print  'bar' to the screen

?>

This is all very well but wouldn't it be easier to give the variables index labels that are easier to remember? Well PHP will allow us to do just that!
PHP Code:
Code:


<?
$arrayVar = array('gazonk' => 'foo', 'bop' => 'bar', 'quux' => 'baz'); //creates the array with easier to remember index values

print $arrayVar['gazonk']; // this would print the word 'foo' to the screen

print $arrayVar['bop']; //this would print  'bar' to the screen

//Note that you can still access the vars in the original way too


print $arrayVar[0]; // this would print the word 'foo' to the screen

print $arrayVar[1]; //this would print  'bar' to the screen

?>

So far we have learned how to create new arrays but we still haven't found out how to add new elements to an array or delete arrays


Creating Array Elements
To create an array element it is simple. Just use the following code


PHP Code:
Code:


<?
$arrayVar = array('gazonk' => 'foo', 'bop' => 'bar', 'quux' => 'baz'); //creates the array with easier to remember index values

//now lets add an element to the array
$arrayVar['bat'] = "zxc";
//note that this new array var has been assigned a numerical index too

//we can also add an array element with only a numerical index by using the following

$arrayVar[] = "gloop"; //this will assign the next available numerical value to 'gloop'
  ?>

That is all you need to add an array variable to an array

Deleting array elements

To delete an array element all you need is it's index and you can remove it using the unset() function. For Example:

PHP Code:
Code:


<?
$arrayVar = array('gazonk' => 'foo', 'bop' => 'bar', 'quux' => 'baz'); //creates the array with easier to remember index values

print $arrayVar['gazonk']; //prints 'foo' to the screen

//we don't need foo anymore so lets delete it
unset($arrayVar['gazonk']);

?>

Conclusion
So what have we done? We have created an array, filled it with elements, created extra elements and deleted elements we no longer need. This knowledge should be sufficient to allow you a good insight into the world of arrays.
__________________
my own tutorial website... i love it... yeah i really do. marry me ..

http://www.tutorials-expert.com
Posted:  14 Nov 2006 22:27
excellent tutorial!

will be adding this to my website

don't worry, credit will be given to you!

thanks!
__________________
http://www.vipercreations.com/images/banner/result.gif
Posted:  15 Nov 2006 01:05
lol... no problem


__________________
my own tutorial website... i love it... yeah i really do. marry me ..

http://www.tutorials-expert.com
Posted:  26 Nov 2006 16:00
Very nice tutorial haxan, really useful mate!! Lots of interesting facts, really cool

Great work!
Posted:  27 Nov 2006 10:30
hehehe... i hope you ppl make it in use..

arrays save some time if you have good control over them.
__________________
my own tutorial website... i love it... yeah i really do. marry me ..

http://www.tutorials-expert.com
Posted:  28 Nov 2006 16:39
nice tut
__________________
http://i53.photobucket.com/albums/g51/spiketmr/sotw9.gif
Posted:  29 Nov 2006 06:15
i really dont know a thing about designing and coding.

is there a way to learn it while you try to build?
Posted:  29 Nov 2006 15:10
yeah...follow tuts for something you want to build
__________________
http://i53.photobucket.com/albums/g51/spiketmr/sotw9.gif