Monday, March 16, 2009

Array declaration, assignment and printing in php

The term array comes in order to store multiple values in a single variable. In php with help of Array you can store multiple values in a single variable instead of defines multiple variables.
Each element in the array is accessed by the associated key value. A key may be either an integer or a string. By default if no key value is mentioned then it starts with 0.

Array declaration Syntax:
An array is created by the array()language construct.
Like,
$arrayName=array('Arju', 'Momin', 'Robert', 'Davis');

Here array key value is omitted. If array key value is omitted then first element is accessed by $variable_name[interger_key_value_start_from_0]
In this case $arrayName[0] have value Arju.
$arrayName[1] have the value Momin and so on.

Example 01:
The following is the php code,

<html>
<body>
<?php
$arrayName= array("Arju","Momin","Robert","Davis");
echo "This is first element :" . $arrayName[0];
echo "<br />";
echo "This is second element :" . $arrayName[1];
?>
</body>
</html>

Output
This is first element :Arju
This is second element :Momin

Example 02:
Below is an example where key value is assigned manually.
If a key is not specified for a value, the maximum of the integer indices is taken and the new key will be that value plus 1.

<html>
<body>
<?php
$arrayName["students"]= 48;
$arrayName[10]=100;
$arrayName[]=120; //120 assigned to key value 11 as maximum key is 10, so it is 10+1
echo "Total Students :" . $arrayName["students"];
echo "<br />";
echo "10th positioned student mark :" . $arrayName[10];
echo "<br />";
echo "11th positioned student mark :" . $arrayName[11];
?>
</body>
</html>

Output

Total Students :48
10th positioned student mark :100
11th positioned student mark :120


Example 03:

You can define multidimensional array by declaring array within an array.
Following is an example.

<html>
<body>
<?php
$student=Array
(
"Seven" => Array
(1 => 76 ,2 => 65 , 3 => 45 ),
"Eight" => Array
(10 => 87 , 89 ),
"Nine" => Array
( 23 => 98, 25 => 86 , 27 => 56 )
) ;

echo "Result of Roll number of 11 of class Eight is: " .$student['Eight'][11];
echo "<br />";
echo "Result of Roll number of 127 of class Nine is: " .$student['Nine'][27];
?>
</body>
</html>


Output
Result of Roll number of 11 of class Eight is: 89
Result of Roll number of 127 of class Nine is: 56

Modifying Array Values
In order to set an array just issue,
$arrayName[key_value]=new_value;
If the variable does not exist array will be created. So it is an another way to create array.

In order to remove element from the array issue,
unset ($arrayName[key_value]);

In order to the array itself issue,
unset $arrayname;

Example:
<?php
$arr = array(1,5);
$arr[] = 8; // This is the same as $arr[2] = 8;
print_r($arr);
$arr["x"] = 42; // Adds a new element to array.
unset($arr[1]); // Removes 2nd element from the array that is 5.
print_r($arr);
unset($arr); // Deletes whole element of the array
print_r($arr);
?>

In order to delete array element one by one issue,

foreach ($arr as $i => $value) {
unset($arr[$i]);
}

Related Documents
Working with cookie in php(set, retrieve, delete)
Accessing data from html forms in php
Track error messages on php
Variables in PHP
Magic constants in php

No comments:

Post a Comment