8. transversing array 2.3
8. transversing array 2.3
CO3 Understand
Students will learn database connectivity, plugin and security
issues.
2
Web Development
Using PHP
Course Outcome • OOP
CO Title Level
Number
• Session and Cookies
Understand
• File handling
CO1
Students will learn basics of PHP • Database
CO2 Understand &
Students will understand the underlying concept of themes in Analyze
wordpress.
CO3 Understand
Students will learn database connectivity, plugin and security
issues.
3
Topics to be Covered
• Traversing arrays
• Sorting
4
Traversing Arrays
• The most common task with arrays is to do something with every element—for
instance, sending mail to each element of an array of addresses, updating each
file in an array of filenames, or adding up each element of an array of prices.
• There are several ways to traverse arrays in PHP, and the one you choose will
depend on your data and the task you're performing.
• The foreach Construct The most common way to loop over elements of an array
is to use the
• foreach construct:
• $addresses = array('[email protected]', '[email protected]');
• foreach ($addresses as $value) { echo "Processing $value\n"; }
• Processing [email protected]
• Processing [email protected]
5
1. Using a for Loop
• For indexed arrays, you can use a for loop with the count() function
• <?php $fruits = ["Apple", "Banana", "Cherry"];
• for ($i = 0; $i < count($fruits); $i++)
• { echo $fruits[$i] . "\n"; }
• // Outputs:
• // Apple
• // Banana
• // Cherry ?>.
6
2. Using a foreach
Loop
The foreach loop is the most common way to traverse arrays and works for both indexed and associative arrays
<?php $fruits = ["Apple", "Banana", "Cherry"];
foreach ($fruits as $fruit) { echo $fruit . "\n"; }
// Outputs:
// Apple
// Banana
// Cherry ?>.
7
3. Using while with each() or
list()
• <?php
• $fruits = ["Apple", "Banana", "Cherry"];
• while ($fruit = current($fruits))
• { echo $fruit . "\n"; next($fruits); }
• // Outputs: // Apple // Banana // Cherry
• ?>
8
The Iterator Functions:-
Every PHP array keeps track of the current element you're working
with; the pointer to the current element is known as the iterator. PHP
has functions to set, move, and reset this iterator. The iterator functions
are:
current( ) Returns the element currently pointed at by the iterator
reset( ) Moves the iterator to the first element in the array and returns
it
next( ) Moves the iterator to the next element in the array and returns it
prev( ) Moves the iterator to the previous element in the array and
returns it
end( ) Moves the iterator to the last element in the array and returns it
9
PHP - Sort Functions For Arrays
we will go through the following PHP array sort functions:
· 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
11
Image References
[1] www.w3resource.com/w3r_images/class-and-object-of-class.gif
[3]www.expertphp.in/images/articles/
ArtImgC67UTd_classes_and_objects.jpg
12
Book References
• Php: The Complete Reference, Steven Holzner, Tata McGraw-
Hill Education, 2007
• Modern PHP: New Features and Good Practices, Josh Lockhart,
"O'Reilly Media, Inc.“, 2015.
• PHP for the Web: Visual QuickStart Guide, Larry Ullman
Pearson Education, 2006.
• https://www.tutorialspoint.com/php/index.htm
• https://www.codecademy.com/learn/learn-php
13
Video Links
• https://www.youtube.com/watch?v=D7mqV-p1kEc
• https://www.youtube.com/watch?v=BUCiSSyIGGU
14
THANK YOU