TOP TECH
  Learn How to.....
Welcome IT Related Articles Member Area  |  Sign In  |  Sign Up
.: Home :.
General
HTML
CSS
PHP
PHP-MySQL
FrontPage
Photoshop
CorelDRAW
PageMaker
MS Office
VisualBasic
All
.

.: Home :.

Home >> PHP - MySQL
Programming with PHP 2.2 [Page 2]
Article by: Shelton Manage
Wednesday, 03rd June, 2009


Creating Arrays

There are two primary ways to define your own array. First you could add an element at a time to build one.

$array[] = 'value';
$array[] = 'another value';
$array[key] = 'more values';

It's important to undustand that if you specify a key and a value already excits indexes with that same key, the new value will averwrite the excisting one.

$array['name'] = 'Prasad';
$array['name'] = 'Ujitha';
$array[2] = 'Shelee';
$array[2] = 'Manel';

You can use the array() function to build an entire array in one step:
$array = array('key' => 'value', 'key2' => 'value2');

This function can be used whether or not you explicitly set the key.
$array = array ('value', 'value2', 'value3');

Or, if you set the first numeric keys value, the added values will be keyed incrementaly thereafter:
$days = array (1 => 'Sunday', 'Monday', 'tuesday');
echo $days[3]; // Tuesday

Finally, If you want to create an array of sequential numbers, you can use the range() function.
$ten = rang (1, 10);

Now you know how to access individual array elements by key (eg. $_POST['price']). To access every array element, use the foreach loop:

foreach ($array as $value) {
// Do something.
}

To access both the key and values use

foreach ($array as key => $value){
echo " The array value at $key is $value.";
}



Page: First - 1 2 3 4 5 6 - Last

 



Comments


© 2009 Top Sri Lanka Top Tech.
Articles Manager β - Script by: DynamicCodes.com