Monday, September 20, 2010

PHP:array ||php array syntax example with output

PHP Arrays


An array is a data structure that stores one or more values in a single value. For experienced programmers it is important to note that PHP’s arrays are actually maps (each key is mapped to a value).

PHP Arrays provide a way to group together many variables such that they can be referenced and manipulated using a single variable. An array is, in many ways, a self-contained list of variables.

Once an array has been created items can be added, removed and modified, sorted and much more. The items in an array can be of any variable type, and an array can contain any mixture of data types – it is not necessary to have each element in the array of the same type.

What is an array?

When working with PHP, sooner or later, you might want to create many similar variables.

Arrays are special data types. Despite of other normal variables an array can store more than one value. Let’s suppose you want to store basic colors in your PHP script. You can construct a small list from them like this: Color list: * red * green * blue * black * white It is quite hard, boring, and bad idea to store each color in a separate variable. It would be very nice to have the above representation almost as it is in PHP. And here comes array into play. The array type exists exactly for such purposes. So let’s see how to create an array to store our color list.

There are three different kind of arrays:

• Numeric array - An array with a numeric ID key
• Associative array - An array where each ID key is associated with a value
• Multidimensional array - An array containing one or more arrays

Creating a PHP Array

Arrays are created using the array() function. The array() function takes zero or more arguments and returns the new array which is assigned to a variable using the assigment operator (=). If arguments are provided they are used to initialize the array with data.

PHP arrays grow and shrink dynamically as items are added and removed so it is not necessary to specify the array size at creation time as it is with some other programming languages.

We can create an empty array as follows:

     $citylist = array();
?>

Alternatively, we can create an array pre-initialized with values by providing the values as arguments to the array() function:

     $citylist = array("Noida", "Delhi", "Raipur", "Ambikapur", "Bhagalpur");
?>

Accessing Elements in a PHP Array

The elements in a PHP numerical key type array are accessed by referencing the variable containing the array, followed by the index into array of the required element enclosed in square brackets ([]). We can extend our previous example to display the value contained in the second element of the array (remember that the index is zero based so the first element is element 0):

 $citylist = array("Noida", "Delhi", "Raipur", "Ambikapur", "Bhagalpur");
echo $citylist[1];
?>

The above echo command will output the value in index postion 1 of the array, in this case Output will be:

Delhi

Manipulating Array

Changing, Adding and Removing PHP Array Elements

An array element can be changed by assigning a new value to it using the appropriate index key.

For example, to change the first element of an array:


Before changing the Array, element [0] : ".$citylist[0];
$citylist[0]= "Faridabad";
echo "
After changing the Array , element [0] : ".$citylist[0];
?>

Output:

img

A new item can be added to the end of an array using the array_push() function. Push one or more elements onto the end of array

Syntax:

int array_push ( array array, mixed var [, mixed ...] )

array_push() treats array as a stack, and pushes the passed variables onto the end of array. The length of array increases by the number of variables pushed. This function takes two arguments, the first being the name of the array and the second the value to be added:

Example:

";
print_r($citylist);
array_push($citylist, "Gurgaon");
echo "

After changing the Array
";
print_r($citylist);
echo "
";
?>

Output:

img

The first element of the array can be removed of the array using the array_shift() function, shifts the first value of the array off and returns it, shortening the array by one element and moving everything down..

Syntax:

mixed array_shift ( array )

";
print_r($citylist);
echo "
";
array_shift($citylist);// Add Gurgaon to start of array
echo "After Removing the element [0] from array";
echo "
";
print_r($citylist);
echo "
";
?>

Output:

Before Removing the element [0] from array
Array
(
[0] => Noida
[1] => Delhi
[2] => Raipur
[3] => Ambikapur
[4] => Bhagalpur
)
After Removing the element [0] from array
Array
(
[0] => Delhi
[1] => Raipur
[2] => Ambikapur
[3] => Bhagalpur
)

The last item added to an array can be removed from the array using the array_pop() function.

Syntax:

mixed array_pop ( array array )

The following example removes the last element added:

";
print_r($citylist);
echo "
";
array_push($citylist, "Jaipur"); // Add Jaipur to end of array
echo "After Adding an element to the array";
echo "
";
print_r($citylist);
echo "
";
array_pop($citylist); // Remove Jaipur from the end of the array
echo "After Removing an element from array";
echo "
";
print_r($citylist);
echo "
";
?>

Output:

Try it yourself!

An element can be added to the start of the array by using array_unshift() function:

SYNTAX


";
print_r($citylist);
echo "
";
array_shift($citylist);// Add Gurgaon to start of array
echo "After Removing the element [0] from array";
echo "
";
print_r($citylist);
echo "
";
array_unshift($citylist,"Mumbai") ;
echo "After Adding Mumbai to the start of the array";
echo "
";
print_r($citylist);
echo "
";
?>

Output:

Original Array
Array
(
[0] => Noida
[1] => Delhi
[2] => Raipur
[3] => Ambikapur
[4] => Bhagalpur
)
After Removing the element [0] from array
Array
(
[0] => Delhi
[1] => Raipur
[2] => Ambikapur
[3] => Bhagalpur
)
After Adding Mumbai to the start of the array
Array
(
[0] => Mumbai
[1] => Delhi
[2] => Raipur
[3] => Ambikapur
[4] => Bhagalpur
)

Looping through PHP Array Elements

It is often necessary to loop through each element of an array to either read or change the values contained therein. There are a number of ways that this can be done.

One such mechanism is to use the foreach loop. The foreach loop works much like a for or while loop and allows you to iterate through each array element. There are two ways to use foreach. The first assigns the value of the current element to a specified variable which can then be accessed in the body of the loop.

The syntax for this is:

foreach ($arrayName as $variable)

For example:


";
}
?>

Will result in the following output:

Noida
Delhi
Raipur
Ambikapur
Bhagalpur

For associative arrays the foreach keyword allows you to iterate through both the keys and the values using the following syntax:

foreach ($arrayName as $variable)

For example:

     $employees = array('empName'=>
'Sanjay Singh', 'empAddr'=>'1 The Street',
'accountNumber'=>'123456789');
echo “
Employee Name
Address A/C No”

foreach ($employees as $key=>$value)
{
echo " $key = $value
";
}

?>

Output:

Try it yourself


Read more: http://funrockerz.com/2679.html#ixzz10376XXku

No comments:

Post a Comment