(Lesson 7) Arrays
(Lesson 7) Arrays
An array is a special variable (object), which can hold more than one value
at a time.
An array stores multiple values in one single object.
In PHP, there are three types of arrays:
Indexed arrays - Arrays with a numeric index
The index of an array refers to the location of an item within the array.
The index always start from 0.
So, an array of 100 items, has an index starting from 0 to 99.
The indices can be assigned automatically, like this:
$names = array("Ahmed", "Sarah", "Youssef"); → Array containing three strings
$grades = array(87, 95, 68, 94, 25, 76); → Array containing six integers
The indices can also be assigned manually, like this:
$names[0] = "Ahmed";
$names[1] = "Sarah";
$names[2] = "Youssef";
Example: defining and assigning values to an array
The following example creates an indexed array called $names, assigns three
elements to it, and then prints a message containing the array values:
<html>
<body>
<?php
$names = array("Ahmed", "Sarah", "Youssef");
echo "<h3>Employees:</h3><ol>
<li>$names[0]</li> <li>$names[1]</li>
<li>$names[2]</li></ol>";
?>
</body>
</html>
Tip: ARRAYS should be typically handled via LOOPS. Let’s see how …
Handling an array using “for” loop
Looping is the typical way to deal with an array, no matter how big it is.
Handling an array using “while” and “do while”
Of course, we can also print the elements of an array using “while” and “do while” loops:
<html> <html>
<body> <body>
<?php <?php
$names = array("Ali", "Mai", "Adel", "John", "Mary"); $names = array("Ali", "Mai", "Adel", "John", "Mary");
echo "<h3>Employees:</h3><ol>"; echo "<h3>Employees:</h3><ol>";
$i=0; $i=0;
while($i<=4) do
{ {
echo "<li> $names[$i]</li>"; echo "<li> $names[$i]</li>";
$i++; $i++;
} } while($i<=4);
echo"</ol>"; echo"</ol>";
?> ?>
</body> </body>
</html> </html>
Handling an array using “foreach” loop
The user inputs ten values. The program calculates and prints their average.
<html> <html>
<body> <body>
<form action= "example2.php"> <table> <?php
<?php $Arr = $_GET["num"];
for($i=1; $i<=10; $i++) $sum = 0;
echo "<tr><td>Grade ($i)</td> foreach($Arr as $val)
<td><input type=text name=num[]></td></tr>"; {
?> $sum = $sum + $val;
<tr><td></td> }
<td><input type= "submit" value= "Get Average"></td></tr> $avg = $sum / 10;
</table> </form> echo "<h3>The average is $avg</h3>";
</body> ?>
</html> </body>
</html>
Input form (form2.php) PHP Program (example2.php)
Example: Calculating the average
Example (1): Adding up two different arrays
<html>
<body>
<form action= "total.php">
<table><tr><th>Student ID</th><th>Exam1</th>
<th>Exam2</th></tr>
<?php
for($i=1; $i<=5; $i++)
echo "<tr><td>Student ($i)</td>
<td><input type=text name=exam1[]></td>
<td><input type=text name=exam2[]></td></tr>";
?>
<tr><td><input type= "submit" value= "Calculate
Total"></td></tr>
</table> </form>
</body>
</html>
Input form (form1.php)
Example (1): Adding up two different arrays
<html>
<body>
<table border=1 cellspacing=0><tr><th>Exam1</th>
<th>Exam2</th><th>Total</th></tr>
<?php
$exam1 = $_GET["exam1"];
$exam2 = $_GET["exam2"];
for($i=0; $i<=4; $i++)
{
$total[$i] = $exam1[$i] + $exam2[$i];
echo"<tr><td>$exam1[$i]</td><td>$exam2[$i]
</td> <td>$total[$i]</td></tr>";
}
?>
</body>
</html>
PHP Program (total.php)
Example (2): Getting the maximum value
<html>
<body>
<?php
$Arr = $_GET["num"];
$max = $Arr[0];
foreach($Arr as $val)
{
if($val>$max)
$max = $val;
}
echo "<h3>The highest grade is $max</h3>";
?>
</body>
</html>
<html><body>
<?php
$name = array("Bassem", "Dina", "Ahmed", "Carol");
$mark = array(76, 93, 65, 97, 55, 82, 42);
sort($name);
rsort($mark);
echo "<h3>Sorted Names: (ascending):</h3><ol>";
for($i=0; $i<count($name); $i++)
echo"<li>$name[$i]</li>";
echo"</ol>";
echo"<h3>Sorted Numbers (descending):</h3> <ol>";
foreach($mark as $x)
echo"<li>$x</li>";
echo"</ol>";
?>
</body></html>
Array’s Functions: shuffle
<html>
<body>
<?php
$name = array("Bassem", "Dina", "Ahmed", "Carol");
shuffle($name);
echo "<h3>Names shuffled randomly:</h3><ul>";
foreach($name as $val)
echo"<li>$val</li>";
echo"</ul>";
shuffle($name);
echo "<h3>Names shuffled again:</h3><ul>";
foreach($name as $val)
echo"<li>$val</li>";
echo"</ul>";
?>
</body>
</html>
Search, read, and learn more about:
array_reverse()
array_search()
array_sum()