Introduction to PHP – Part7

advertisement

advertisement

Array:

Array is a systematic collection of data of similar data types that can be accessed by numeric index. Array is one of the most important and oldest data structures that are used to implement other data structures like strings, maps, vector etc. An array in PHP is in fact an ordered map type that associates values to keys. We suggest readers to not get confused with map type here, simply understand PHP array as a data type that can be accessed using keys.

In a situation where it is almost impossible to create a number of variables, a single array can solve the problem. For example, we need to store marks of 100 students having Roll no 1 to 100. Instead of creating 100 variables student1, student2, ……., student100 we can create an array of size 100. After we created array, the Roll no can be used as index. More clearly, mark of student with Roll no 1 can be a stored and accessed using index 1, i.e. student[1].

Creating Array

Syntax

array() is used to create an array. We can specify any number of parameters of key => value pairs separated by comma.

array(  key1 =>  value1, key2 =>value 2,..............keyn => valuen)

Key can only be number or string, but value can be any type, it can be an array, map, vector, object or any other PHP supported type.

Example#1

<html>
<body>
<?php
/*
Output: 59 69 90 84 73 79 33 66 93 54
Average marks: 70
Marks of Roll No 5: 73
Program description: This program creates an array called student with 10 elements
and stores marks obtained by each student. The code inside the foreach loop print the marks obtained
by each student and adds to the totalMarks. After foreach loop the Average marks is calculated.
*/
$student  = array(1 => 59, 2 => 69, 3 => 90, 4 => 84, 5 => 73, 6 => 79, 7 => 33, 8 => 66, 9 => 93, 10 => 54);
/*
This array can also be defined as
$student = array(59, 69, 90, 84, 73, 79, 33, 66, 93, 54);
If you want to start index from a specific number for example say 100, you can write like
$student = array(100 => 59, 69, 90, 84, 73, 79, 33, 66, 93, 54);
*/
$totalMarks=0;
foreach($student as $marks)
{
         echo $marks." ";
         $totalMarks+=$marks;
}
echo "<br/> Average marks: ". $totalMarks/10;

/*
It is also possible to access the array element using the numeric index,
for example to print the marks of student with rollno 5 can be printed as
*/
echo "<br/> Marks of Roll No 5 : ". $student[5];
?>
</body>

The array we used in example has a numeric index or key. We can also implement array with string key, this array is known as associative array very similar to map data structure.

Example#2

<html>
<body>
<?php
/*
Output: 59 69 90 84 73 79 33 66 93 54
Average marks: 70
Marks of Sam: 69
Program description: This program creates an array called student with 10 elements
and stores marks obtained by each student. The code inside the foreach loop print the marks obtained
by each student and adds to the totalMarks. After foreach loop the Average marks is calculated.
*/

$student  = array("Hary" => 59, "Sam" => 69, "Joe" => 90, "Kevin" => 84, "Kelly" => 73, "Steve" => 79,
 "Craig" => 33, "Jodi" => 66, "John"=> 93, "Rose" => 54);
$totalMarks=0;
foreach($student as $marks)
{
            echo $marks." ";
            $totalMarks+=$marks;
}
echo "<br/> Average marks: ". $totalMarks/10;

/*
It is also possible to access the array element using the string key,
for example to print the marks of "Sam" can be printed as
*/
echo "<br/> Marks of Sam : ". $student["Sam"];
?>
</body>

Modifying array

To modify an existing array, we can assign values explicitly to the array. Remember that, if the specified array does not exist yet, a new array will be created. The following example shows how to modify an existing array.

Example#3

<html>
<body>
<?php
/*
This program creates an array called countries with two elements
*/
$countries  = array(1 => "Australia", 2 => "New Zealand");
// To add a country called Japan, we need to assigh it explicitly as
$countries[3] = "Japan";
// We can aslo add a element without specifying the key, in this situation
// the key of new element will he the key of last element + 1
$countries[] = "United Kingdom";
 
//To simply print an array we can use the function print_r($array) as follows
// Output: Array ( [1] => Australia [2] => New Zealand [3] => Japan [4] => United Kingdom )
print_r($countries);

/* Deleting element from array:
Output:  Array ( [1] => Australia [3] => Japan [4] => United Kingdom )
Remember that the key of Japan and United Kingdom does not get changed
*/
unset($countries[2]);
print_r($countries);

// This will delete the whole array
unset($countries);
//If we try to print the array, a message like the following will be displayed
//Undefined variable: countries in C:\wamp\www\WebTest\index.php on line 29
print_r($countries);
?>
</body>

For a complete reference of array please visit http://www.php.net/manual/en/language.types.array.php and for complete reference on array functions please visit http://www.w3schools.com/PHP/php_ref_array.asp

About the Author:

Bhoj R Dhakal is a programmer. He has few years of experience in software development and maintenance. He has published series of PHP tutorials for webanddesigners.