PHP
Arrays
Arrays
• An array is a special variable, which can hold
more than one value at a time.
• array() function is used to create an array.
• An array can hold many values under a single
name, and you can access the values by referring
to an index number.
<?php
$students = array(“Ali", “Saeed", “Bilal");
echo "Students Names are <br> " . $students[0] .
", " . $students[1] . ", " . $students[2] ;
?>
Types of Arrays
In PHP, there are three types of arrays:
• Indexed arrays - Arrays with a numeric index
• Associative arrays - Arrays with named keys
• Multidimensional arrays - Arrays containing
one or more arrays
Indexed Arrays
• There are two ways to create indexed arrays:
• The index can be assigned automatically (index
always starts at 0), like this:
$students = array(“Ali", “Saeed", “Bilal");
• or the index can be assigned manually:
$students[0] = “Ali";
$students[1] = “Saeed";
$students[2] = “Bilal";
Indexed Arrays (Cont…)
<?php
$students = array("Rizwan", "Gohar", "Ijaz");
$arrlength = count($students); //count()
returns length of array
for($x = 0; $x < $arrlength; $x++)
{
echo $students[$x];
echo "<br>";
}
?>
Associative Arrays
• These arrays use named keys assigned to them.
• There are two ways to create an associative array:
$gpa = array(“Rizwan"=>"3.5", “Gohar"=>"3.6",
“Ijaz"=>“3.4");
• Or
$gpa[‘Rizwan'] = "3.5";
$gpa[‘Gohar'] = "3.6";
$gpa[‘Ijaz'] = "3.4";
Associative Arrays (Cont…)
• foreach loop is used to loop through an
Associative array.
<?php
$gpa = array("Rizwan"=>"3.5", "Gohar"=>"3.6",
"Ijaz"=>"3.4");
foreach($gpa as $x => $x_value)
{
echo "Key=" . $x . ", Value=" . $x_value. "<br>";
}
?>
Multidimensional Arrays
• A multidimensional array is an array containing
one or more arrays.
• The dimension of an array indicates the number
of indices you need to select an element.
A two-dimensional array is an array of arrays
A three-dimensional array is an array of arrays
of arrays
For a two-dimensional array you need two
indices to select an element
For a three-dimensional array you need three
indices to select an element
Two-dimensional Arrays
• A two-dimensional array is an array of arrays
Name Semester GPA
Ali 1 3.2
Zia 3 3.3
Naveed 5 3.4
• We can store the data from the table above in a two-
dimensional array, like this:
$students = array
(
array(“Ali",1,3.2),
array(“Zia",3,3.3),
array(“Naveed",5,3.4),
);
Two-dimensional Arrays (Cont…)
<?php
$students = array(array("Ali",1,3.2),
array("Zia",3,3.3),array("Naveed",5,3.4),);
for ($row = 0; $row < 3; $row++) {
echo "<p><b>Row number $row</b></p>";
echo "<ul>";
for ($col = 0; $col < 3; $col++) {
echo "<li>".$students[$row][$col]."</li>";
}
echo "</ul>";
}
?>
Sort Functions For Arrays
• sort() - sort indexed arrays in ascending order
• rsort() - sort indexed arrays in descending
order
• asort() - sort associative arrays in ascending
order, according to the value
• ksort() - sort associative arrays in ascending
order, according to the key
• arsort() - sort associative arrays in
descending order, according to the value
• krsort() - sort associative arrays in
descending order, according to the key
Sort indexed Array in Ascending Order
sort()
• Sorting indexed array in ascending alphabetical order:
<?php
$students = array(“C", “G", “A");
sort($students);
$arrlength = count($students);
for($x = 0; $x < $arrlength; $x++) {
echo $students[$x];
echo "<br>";
}
?>
Sort indexed Array in Descending Order -
rsort()
• Sorting indexed array in descending alphabetical order:
<?php
$students = array(“C", "G", “A");
rsort($students);
$arrlength = count($students);
for($x = 0; $x < $arrlength; $x++) {
echo $students[$x];
echo "<br>";
}
?>
Sort Associative Array (Ascending Order),
According to Value - asort()
<?php
$gpa = array("Rizwan"=>"3.5",
"Gohar"=>"3.6", "Ijaz"=>"3.4");
asort($gpa);
foreach($gpa as $x => $x_value) {
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
?>
Sort Array (Ascending Order), According to
Key - ksort()
<?php
$gpa = array(“C"=>"3.5", "G"=>"3.6",
“A"=>"3.4");
ksort($gpa);
foreach($gpa as $x => $x_value) {
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
?>
Sort Array (Descending Order), According to
Value - arsort()
<?php
$gpa = array("C"=>"3.5", "G"=>"3.6",
"A"=>"3.4");
arsort($gpa);
foreach($gpa as $x => $x_value) {
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
?>
Sort Array (Descending Order), According to
Key - krsort()
<?php
$gpa = array("C"=>"3.5", "G"=>"3.6",
"A"=>"3.4",);
krsort($gpa);
foreach($gpa as $x => $x_value) {
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
?>
Break……
Destructor
• A destructor is called when the object is destructed or the script is
stopped or exited.
• If you create a __destruct() function, PHP will automatically call this
function at the end of the script.
• Notice that the destruct function starts with two underscores (__).
<?php
class Fruit {
public $name;
public $color;
function __construct($name) {
$this->name = $name;
}
function __destruct() {
echo "The fruit is {$this->name}.";
}
}
$apple = new Fruit("Apple");
?>
The final Keyword
• The final keyword can be used to prevent class
inheritance.
• It will generate the following error.
<?php
final class Fruit {
}
class Strawberry extends Fruit {
}
?>
Fatal error: Class Strawberry may not inherit from
final class (Fruit)
Abstract Classes
• An abstract class is one that cannot be
instantiated, only inherited. You declare an
abstract class with the keyword abstract, like
this −
• When inheriting from an abstract class, all
methods marked abstract in the parent's class
declaration must be defined by the child;
additionally, these methods must be defined
with the same visibility.
abstract class MyClass {
abstract function myFunction() {
}
}