Array(I)Record
Array(I)Record
Arrays
12/17/2024 2
Learning Outcomes
• To learn in an array, accessing an element is very easy by using the
index number
• The search process can be applied to an array easily.
• How can use 2D array is used to represent matrices
• For any reason a user wishes to store multiple values of similar type
then the Array can be used and utilized efficiently
12/17/2024 3
Contents
• Creating Arrays
• Accessing Array Elements
• Looping Through Arrays with foreach
12/17/2024 4
Arrays
• An array stores multiple values in one single variable.
• An array is a special variable, which can hold more than one value
at a time.
• An array can hold many values under a single name, and can access
the values by referring to an index number.
• Information in an array can be handled, accessed and modified
easily.
12/17/2024 5
Creating Element
• In PHP, the array( ) function is used to create an array:
$myArray=array ( values ) ;
• The index start from 0 and increments by 1 from left to right of the array.
12/17/2024 6
Accessing Arrays Element
• There are three type of arrays:
• Index array(Numeric array) - An array with a numeric index
• Associative array - An array where each ID key is associated
with a value
• Multidimensional array - An array containing one or more arrays
12/17/2024 7
Accessing Arrays Element
Index array (or)Numeric array
• A numeric array stores each element with a numeric ID
key(numeric index).
• Numeric arrays allow us to store multiple values of the same data
type in a single variable without having to create separate
variables for each value.
12/17/2024 8
Accessing Arrays Element
Index array (or) Numeric array
• There are two ways to create index arrays:
• The index can be assigned automatically (index always start at 0)
$name = array ("Value1", “Value2", “Value3") ;
Example: $season=array ("summer", "winter", "spring", "autumn");
(or)
$arrayname[‘key1’] = value1; $salary['Peter'] = "350000";
$arrayname[‘key2’] = value2; $salary['Benn'] = “450000";
$salary['John'] = “200000";
$arrayname[‘key3’] = value3;
12/17/2024 13
Accessing Arrays Element
Example:
<!DOCTYPE html> Output:
<html> Peter is 35 years old.
<body> Benn is 37 years old.
<?php John is 43 years old.
$age = array("Peter"=>"35", "Benn"=>“37",
John"=>“43");
echo "Peter is " . $age['Peter'] . " years old.” “<br/>”;
echo “Benn is " . $age[‘Benn'] . " years old."“<br/>”;
echo “John is " . $age[‘John'] . " years old.";
?>
</body></html>
<?php Output:
$age = array("Peter"=>"35", "Benn"=>"37", "John"=>"43"); Array ( [Peter] => 35
print_r($age); [Benn] =>
?> 37 [John] => 43
12/17/2024 ) 14
Accessing Arrays Element
Example:
<!DOCTYPE html> Output:
<html> Peter
<body> salary: 350000
<?php Benn
$salary[“Peter"]="350000"; salary: 450000
$salary[“Benn"]="450000"; John salary:
$salary["John"]="200000"; 200000
echo “Peter salary: ".$salary[“Peter"].“ <br/>";
echo “Benn salary: ".$salary[“Benn"].“<br/>";
echo “John salary: ".$salary["John"].“;
?>
</body>
</html>
12/17/2024 15
Looping Through Arrays with
foreach
• The foreach()method is used to loop through the elements in an
indexed or associative array.
• It can also be used to iterate over objects. This allows to run blocks
of code for each element.
• The foreach()method has two syntaxes, one for each type of array.
• The syntax for indexed arrays is as given in the following code block:
foreach (iterable as $value)
12/17/2024 16
Looping Through Arrays with foreach
Foreach on Indexed Arrays:
<html> Output: Size is: Big
<body> Size is: Medium
<?php Size is: Short
$size=array(“Big”, “Medium",
“Short");
foreach ($size as $value)
{
echo “Size is: $value<br/>”;
}
?>
</body>
</html>
12/17/2024 17
Looping Through Arrays with
foreach
Foreach on Associative Arrays:
<!DOCTYPE html> Output:
<html> name: Peter
<body> occupation: actor
<?php age: 30
$address['name'] = "Peter";
$address['occupation'] = "actor";
$address['age'] = "30";
foreach($address as $key=>$value)
{
echo "<b>$key:</b> $value<br/>";
} ?>
</body></html>
12/17/2024 18
Looping Through Arrays with foreach
Foreach on Associative Arrays:
<html> Output:
<body> Key: Peter
<?php Value:35
$age=array("Peter"=>"35","Benn"=>"37","John"=>"43"); Key: Benn
foreach($age as $k => $value) Value:37
{ Key: John
echo "Key: " . $k . "Value: " . $value; Value:43
echo "<br/>";
}
?>
</body>
</html>
12/17/2024 19
Outputing an Entire Array with
print_r
• Can see the structure and values of any array by using one of two statements :
print_r or var_dump.
•
<?php
$persons = array("Mary" => "Female", "John" => "Male", “Emma" =>
"Female");
print_r($persons);
echo "<br/>";
echo "Mary is a " . $persons["Mary"];
?>
Array ( [Mary] => Female [John] => Male [Emma] => Female )
Mary is a Female
12/17/2024 20
Outputing an Entire Array with
var_dump()
• The function var_dump () displays structured information (type and value) about
one or more expressions/variables.
<?php Output:
$a = 32; int(32)
echo var_dump($a) . "<br>"; string(12) "Hello world!“
$b = "Hello World!"; float(32.5)
echo var_dump($b) . "<br>"; array(3) { [0]=> string(3) "red" [1]=> string(5)
$c = 32.5; "green" [2]=> string(4) "blue" }
echo var_dump($c) . "<br>"; array(4) { [0]=> int(32) [1]=> string(12) "Hello
$d = array("red", "green", "blue"); world!" [2]=> float(32.5) [3]=> array(3)
echo var_dump($d) . "<br>"; { [0]=> string(3) "red" [1]=> string(5) "green"
$e = array(32, "Hello world!", 32.5, array("red", "green", [2]=> string(4) "blue" } }
"blue")); int(32) string(12) "Hello world!"
echo var_dump($e) . "<br>";
// Dump two variables
echo var_dump($a, $b) . "<br>";
?>
12/17/2024 21
References
• Beginning PHP 5.3, 1st Edition by Matt Doyle
• PHP 5 for Dummies by Janet Valade
• PHP Refreshment Course from UCSY
12/17/2024 22
University of Computer Studies
THANK YOU
12/17/2024 23