Web Designing WAP development EWAVEZ Web Designs Software Development EWAVEZ Home About Us Contact Us PHP Tutorial
Multimedia
Web Development  
Web Hosting
Multimedia development
Software Development
Web Solutions
Our Clientele
     
 
 

PHP-Tutorial

 
 

PHP Arrays

When i first started learning PHP arrays confused me. I learned Perl first; arrays and variables in Perl are represented with different symbols. Not so in PHP, an array looks exactly the same as a variable, it starts with the dollar ($) sign.

$array = array('cat', 'dog', 'fish', 'rabbit');

Think of an array as a variable that can store many separate values. We can access each item in the array.

echo $array[0]; //returns "cat"
echo $array[1]; //returns "dog"
echo $array[2]; //returns "fish"
echo $array[3]; //returns "rabbit"

Now each item of the array is accessed using the variable name, followed directly by square brackets containing the number of the item to be accessed. The number listing of the array starts at zero, so the first item is 0, second is 1, third is 2, etc.

Association

Let's have a look a little deeper into arrays. Arrays have a "key" and a "value". The key is what you use to access the value.

echo $array[1]; // returns "dog"

$array[KEY] returns the VALUE. 0 is the key, and "dog" is the value.

$array = array(0 => 'cat', 1 => 'dog', 2 => 'fish', 3 => 'rabbit', );

See how each number(key) has an association with it's value? The "keys" don't have to be numbers. If the keys aren't defined, they will default to integers to represent each array item. Let's define another array.

$array = array('name' => 'Jester',
'lives' => 'England',
'occupation' => 'bum',
'fave_food' => 'pie');

Now we can use the key associated with it's value to return the values:

echo $array['name']; // returns "Jester"
echo $array['lives']; // returns "England"
echo $array['occupation']; // returns "bum"
echo $array['fave_food']; // returns "pie"

This works like associative arrays in Perl, the methods are almost identical. Arrays don't stop here, they can become very complex, epecially when we get into multi-dimensional arrays.

Multi-dimensional Arrays

We can define more complex arrays like so:

$array = array(
"names" => array('person1' => 'Jane',
'person2' => 'John',
'person3' => 'Fred',
'person4' => 'Anne'),
"places" => array('place1' => 'America',
'place2' => 'England',
'place3' => 'Australia',
'place4' => 'South Africa'),
"jobs" => array('job1' => 'accountant',
'job2' => 'engineer',
'job3' => 'web designer',
'job4' => 'secretary')
);

The above is a two-dimensional array, it has two "levels" to it. It has three arrays stored within one array. You can access each array within the array by using the KEY associated with a deeper array. You can then use the keys within the deeper arrays to access the array values.

echo $array['names']['person1']; // returns Jane.
echo $array['names']['person3']; // returns Fred.
echo $array['places']['place1']; // returns America.
echo $array['places']['place3']; // returns Australia.
echo $array['jobs']['job1']; // returns accountant.
echo $array['jobs']['job3']; // returns web designer.

As you can see, arrays can become very complex, you could define three, four, five-dimensional arrays.

Environment Variables

In PHP there are certain environment variables available to you in your scripts:

$HTTP_SERVER_VARS['REMOTE_ADDR']; // remote IP
$HTTP_SERVER_VARS['HTTP_USER_AGENT']; // users user-agent string
$HTTP_SERVER_VARS['REMOTE_HOST']; // remote host

Now that you know how arrays work, you can use these environment variables more effectively.

 
Previous Index Next
 
 
 

EWAVEZ Web Designs. Copyright © 2004 All Rights Reserved

| Home | About Us | Contact Us | Terms & Policies | Hosting | Multimedia | Web Designing | Software | Our Clients

 Search