CN5109
WEB APPLICATION
DEVELOPMENT
Chapter 4
PHP Array
PHP Array
• An array is a special variable, which can hold more than one
value at a time.
• If you have a list of items (a list of car names, for example),
storing the cars in single variables could look like this:
$cars1 = "Volvo";
$cars2 = "BMW";
$cars3 = "Toyota";
• However, what if you want to loop through the cars and find a
specific one? And what if you had not 3 cars, but 300?
• An array can hold many values under a single name, and you
can access the values by referring to an index number.
PHP Array
• In PHP, the array() function is used to create an array:
• 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
Syntax:
array();
Indexed Arrays
• There are two ways to create indexed arrays
• The index can be assigned automatically (index always starts
at 0):
• The following example creates an indexed array named $cars,
assigns three elements to it, and then prints a text containing
the array values:
$cars[0] = "Volvo";
$cars[1] = "BMW";
$cars[2] = "Toyota";
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>
Indexed Arrays
• The count() function is used to return the length (the number
of elements) of an array:
<?php
$cars = array("Volvo", "BMW", "Toyota");
$x = count($cars);
Echo “The total is $x”;
?>
Indexed Arrays
• To loop through and print all the values of an indexed array,
you could use a for loop, like this:
<?php
$cars = array("Volvo", "BMW", "Toyota");
$arrlength = count($cars);
for($x = 0; $x < $arrlength; $x++) {
echo $cars[$x];
echo "<br>";
}
?>
Indexed Arrays
• To loop through and print all the values of an indexed array,
you could use a for loop, like this:
<?php
$cars = array("Volvo", "BMW", "Toyota");
$arrlength = count($cars);
for($x = 0; $x < $arrlength; $x++) {
echo $cars[$x];
echo "<br>";
}
?>
Associative Arrays
• Associative arrays are arrays that use named keys that you
assign to them.
• There are two ways to create an associative array:
or
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
$age['Peter'] = "35";
$age['Ben'] = "37";
$age['Joe'] = "43";
Associative Arrays
• The named keys can then be used in a script:
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
echo "Peter is " . $age['Peter'] . " years old.";
?>
Associative Arrays
• To loop through and print all the values of an associative array,
you could use a foreach loop, like this:
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
foreach($age as $x => $x_value) {
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
?>
Try This
<?php
// First method to create array.
$numbers = array( 1, 2, 3, 4, 5);
foreach( $numbers as $value )
{
echo "Value is $value <br />";
}
// Second method to create array.
$numbers[0] = "one";
$numbers[1] = "two";
$numbers[2] = "three";
$numbers[3] = "four";
$numbers[4] = "five";
foreach( $numbers as $value )
{
echo "Value is $value <br />";
}
?>
Try This
<?php
// First method to associate create array.
$salaries = array("mohammad" => 2000, "qadir" => 1000, "zara" => 500);
echo "Salary of mohammad is ". $salaries['mohammad'] . "<br />";
echo "Salary of qadir is ". $salaries['qadir']. "<br />";
echo "Salary of zara is ". $salaries['zara']. "<br />";
// Second method to create array.
$salaries['mohammad'] = "high";
$salaries['qadir'] = "medium";
$salaries['zara'] = "low";
echo "Salary of mohammad is ". $salaries['mohammad'] . "<br />";
echo "Salary of qadir is ". $salaries['qadir']. "<br />";
echo "Salary of zara is ". $salaries['zara']. "<br />";
?>
Sort Functions For Arrays
• The elements in an array can be sorted in alphabetical or
numerical order, descending or ascending.
• sort() - sort arrays in ascending order
• rsort() - sort 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 Functions For Arrays
• Sort arrays in ascending order - alphabetical order
• Sort arrays in ascending order - numerical order
<?php
$cars = array("Volvo", "BMW", "Toyota");
sort($cars);
?>
<?php
$numbers = array(4, 6, 2, 22, 11);
sort($numbers);
?>
Sort Functions For Arrays
• Sort arrays in descending order - alphabetical order
• Sort arrays in descending order - numerical order
<?php
$cars = array("Volvo", "BMW", "Toyota");
rsort($cars);
?>
<?php
$numbers = array(4, 6, 2, 22, 11);
rsort($numbers);
?>
Sort Functions For Arrays
• Sort associative arrays in ascending order, according to the
value
• Sort associative arrays in ascending order, according to the
value
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
asort($age);
?>
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
arsort($age);
?>
Sort Functions For Arrays
• Sort associative arrays in ascending order, according to the key
• Sort associative arrays in descending order, according to the
key
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
ksort($age);
?>
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
krsort($age);
?>
Try This
<?php
$cars = array("Proton","Mazda","Toyota");
sort($cars); //sort array
$arrlength = count($cars);
for ($x=0;$x<$arrlength;$x++)
{
echo $cars[$x];
echo "<br>";
}
?>
Try This
<?php
$age = array("Zack"=>"40", "Bob"=>"37", "John"=>"43");
asort($age); //sort array
foreach($age as $value)
{
echo $value;
echo "<br>";
}
?>
Quiz
a. What are the arrays use for?
b. What are the three type of array?
c. List down any four examples of Array Sort Function.