Monday, February 2, 2009

Creating arrays in PHP

There are two types of arrays
1.Indexed Arrays
2.Associative Arrays

1. Indexed Arrays:
Indexed arrays uses numbers to identify each element

Example:
To build an indexed array the direct way, use numbers instead of strings. Indexed arrays are numbered from 0,
$TestList[0] = 'wine';
$TestList[1] = 'fish';
$TestList[2] = 'bread';
$TestList[3] = 'grapes';
$TestList[4] = 'cheese';

2.Associative Arrays :
Associative arrays use strings to identify each element

Example:
$book['title'] = 'PHP Solutions: Dynamic Web Design Made Easy';
$book['author'] = 'XXXXXXXXXXXXXX';
$book['publisher'] = 'YYYYYYY';
$book['ISBN'] = '1234567';

Although both are perfectly valid ways of creating arrays, it’s a nuisance to have to type out the variable name each time.

Using array() to build an indexed array:
Instead of declaring each array element individually, you declare the variable name
once, and assign all the elements by passing them as a comma-separated list to array()

Example
$TestList= array('wine', 'fish', 'bread', 'grapes', 'cheese');

PHP numbers each array element automatically, beginning from 0, so this creates exactly the same array as if you had numbered them individually.

To add a new element to the end of the array, use a pair of empty square brackets like $TestList[] = 'coffee';
So finally total values becomes $TestList[5].

Using array() to build an associative array

The shorthand way of creating an associative array uses the => operator (an equal sign followed by a greater-than sign) to assign a value to each array key. The basic structure looks like
$arrayName = array('key1' => 'element1', 'key2' => 'element2');
Example:
$book = array('title' => 'PHP Solutions: Dynamic Web Design Made Easy',
'author' => 'XXXXX',
'publisher' => 'YYYYYYY',
'ISBN' => '123456');

Using array() to create an empty array
The very two reasons for creating an empty array are

1.Initializing as array - To create an array so that it’s ready to have elements added to it inside a loop.
2.To clear all elements from an existing array

Example : $TestList= array();

Multidimensional arrays

Array elements can store any data type, including other arrays.
Example:
$books = array(
array(
'title' => 'PHP Solutions: Dynamic Web Design Made Easy',
'author' => 'XXXX',
'publisher' => 'YYYYYY',
'ISBN' => '122122'),
array(
'title' => 'Beginning PHP and MySQL 5',
'author' => 'CCCCCCCC',
'publisher' => 'WWWWW',
'ISBN' => '999999')
);

This example shows associative arrays nested inside an indexed array, but multidimensional arrays can nest either type. To refer to a specific element use the key of both arrays.
for example: $books[1]['author'] -- outputs "CCCCCCCC".

Using print_r() to inspect an array

To check the content of an array during testing, pass the array to print_r()
Example: print_r($books);
OUTPUT will be:
Array
{
[0] => Array
{
'title' => 'PHP Solutions: Dynamic Web Design Made Easy',
'author' => 'XXXX',
'publisher' => 'YYYYYY',
'ISBN' => '122122'),
}
[1] => Array
{
'title' => 'Beginning PHP and MySQL 5',
'author' => 'CCCCCCCC',
'publisher' => 'WWWWW',
'ISBN' => '999999')
}
}

Note:Always use print_r() to inspect arrays; echo and print don’t work. To display the contents of an array in a web page use a foreach loop.

0 comments:

Post a Comment