0% found this document useful (0 votes)
7 views

Array(I)Record

Uploaded by

Nwe Nwe Soe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Array(I)Record

Uploaded by

Nwe Nwe Soe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

University of Computer Studies

Arrays

Department of Information Technology


Supporting and Maintenance
Objectives
• To learn how to use an array don't need to define multiple
variables. By the help of single loop, can traverse all the elements
of an array. Can sort the elements of array.
• To purpose of an array is to store multiple pieces of data of the
same type together.
• To explain how to use an array easier to store elements of same
data type.
• Used to implement other data structures like stack and queue.

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 ) ;

• To access elements of array in PHP, can use array variables for


loop by index which is enclose square bracket.
$element= $array[$index]

• 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");

• The index can be assigned manually:


$name[index]=“value”; $season[0]="summer”;
Example:
$season[1]="winter";
$season[2]="spring";
$season[3]="autumn";
12/17/2024 9
Accessing Arrays Element
Example:
<!DOCTYPE html>
<html>
<body>
<?php
$season=array("summer", "winter", "spring", "autumn");
echo "Season are: $season[0], $season[1], $season[2] and $season[3]";
?>
</body> </html>
Output: Season are: summer, winter, spring and autumn
<?php
$season = array("summer", "winter", "spring", "autumn");
print_r($season);
Output: Array ( [0] => summer [1] => winter [2] => spring [3]=> autumn)
12/17/2024
Accessing Arrays Element
Example:
<!DOCTYPE html> Output:
<html> Season are: summer, winter, spring and
<body> autumn
<?php
$season[0]="summer";
$season[1]="winter";
$season[2]="spring";
$season[3]="autumn";
echo "Season are: $season[0], $season[1],
$season[2] and $season[3]"; ?>
</body> </html>
12/17/2024 11
Accessing Arrays Element
Associative array
• PHP allows to associate name/label with each array element in using =>
symbol
• The key is also referred to as the index. The field names are used as id
keys.
• Associative array are also very useful when retrieving data from the
database.
• An associative array is indexed with strings between the square brackets
rather than numbers.
• Associative array differ from numeric array in the sense that associative
arrays use descriptive names for id keys.
12/17/2024 12
Accessing Arrays Element
Associative array
• There are two ways to create associative arrays:
$name=array (key => value)

$age = array (“Peter"=>"35", "Benn"=>“37", John"=>“43");

(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)

• The syntax for associative arrays:


foreach (iterable as $key => $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

You might also like