SlideShare a Scribd company logo
PHP Arrays
Outline
What are arrays ?
• Arrays are data structures that contain a group of elements
that are accessed through indexes.
• Usage :
<?php
$x = array( “banana”, “orange”, “mango”, 3, 4 );
echo $x[0]; // banana
echo $x[1]; // orange
echo $x[3]; // 3
?>
Ways to work with arrays
<?php
$arr = array( “banana”, “orange”);
$arr[4] = “mango”;
$arr[] = “tomato”;
var_dump($arr);
?>
Printing Arrays
• Use print_r() or var_dump() functions to output the
contents of an array.
<?php
$arr = array( “banana”, “orange”);
var_dump($arr ); // echoes the contents of the array
?>
Enumerative VS. Associative
• Enumerative arrays are indexed using only numerical
indexes.
<?php
$arr = array( “banana”, “orange”);
echo $arr[0]; // banana
?>
Enumerative VS. Associative
• Associative arrays allow the association of an arbitrary key
to every element.
<?php
$arr = array( ‘first’ => “banana”,
‘second’ =>“orange”);
echo $arr[‘first’]; // banana
?>
Multidimensional Arrays
• Multidimensional arrays are arrays that contain other
arrays.
<?php
$arr = array(
array( ‘burger’, 5, 15 ),
array( ‘cola’, 2, 25 ),
array( ‘Juice’, 3, 7 ),
);
echo $arr[0][0]; // burger
?>
Title Price Quantity
Burger 5 15
Cola 2 25
Juice 3 7
Array Iteration
• foreach loop :
Loop through arrays.
<?php
$arr1 = array(“mango" , “banana" , “tomato”);
foreach( $arr1 as $key => $value ){
echo $key . “ ----- > “ . $value . “<br/>”;
}
?>
Array Iteration
• We can use any other loop to go through arrays:
<?php
$arr1 = array(“mango" , “banana" , “tomato”);
for( $i =0; $i < count($arr1) ; $i++ ){
echo $i . “ ----- > “ . $arr1 [$i] . “<br/>”;
}
?>
Class Exercise
• Using arrays, write a PHP snippet that outputs the
following table data in an HTML table and calculate the
“total price” column values :
Title Price Quantity Total Price
Burger 5 10
Cola 2 4
Juice 3 7
Milk 2 6
Class Exercise - Solution
<?php
$array = array(
array( "name" => "Burger",
"price" => 5,
"quantity" => 10
),
array( "name" => "Cola",
"price" => 2,
"quantity" => 4
),
array( "name" => "Juice",
"price" => 3,
"quantity" => 7
),
array( "name" => "Milk",
"price" => 2,
"quantity" => 6
)
);
( Continued in the next slide )
Class Exercise - Solution
echo '<table border="1">';
echo "<tr><th>Name</th><th>Price</th><th>Quantity</th><th>Total Price</th></tr>";
foreach( $array as $row ){
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['price'] . "</td>";
echo "<td>" . $row['quantity'] . "</td>";
echo "<td>" . ( $row['price'] * $row['quantity'] ) . "</td>";
echo "</tr>";
}
echo "</table>";
?>
Array Operations
• PHP has a vast number of functions that allow us to do
many operations on arrays.
• For a complete reference of the functions, visit
http://php.net/manual/en/ref.array.php.
Array Comparison
• Equality ‘==‘:
True if the keys and values are equal.
<?php
$arr1 = array( “banana”, “mango”);
$arr2 = array( “banana”, “tomato”);
$arr3 = array( 1=> “mango”, 0=> “banana” );
if($arr1 == $arr2 ) // false
if($arr1 == $arr3 ) // true
?>
Array Comparison
• Identical ‘===‘:
True if the keys and values are equal and are in the same
order.
<?php
$arr1 = array( “banana”, “mango”);
$arr3 = array( 1=> “mango”, 0=> “banana” );
if($arr1 === $arr3 ) // false
?>
Array Comparison
• array array_diff ( array $array1 , array $array2 [, array
$ ... ] ) :
Returns an array containing all the entries from array1 that
are not present in any of the other arrays.
<?php
$arr1 = array( “banana”, “mango”, “lemon”);
$arr2 = array( “banana”, “mango”);
$diff = array_diff($arr1, $arr2); // lemon
?>
Counting Arrays
• int count ( mixed $var [, int $mode =
COUNT_NORMAL ] ) :
Count all elements in an array.
<?php
$arr1 = array( “banana”, “mango”, “lemon”);
echo count($arr1); // 3
?>
Searching Arrays
• mixed array_search ( mixed $needle , array $haystack [,
bool $strict ] ) :
Searches the array for a given value and returns the
corresponding key if successful.
<?php
$arr1 = array( “banana”, “mango”, “lemon”);
echo array_search( 'mango', $arr1 ); // 1
echo array_search( ‘strawberry’, $arr1 ); // false
?>
Deleting Items
• void unset ( mixed $var [, mixed $var [, mixed $... ]] ):
Unset a given variable.
<?php
$arr1 = array( “banana”, “mango”, “lemon”);
unset( $arr1[0] );
var_dump($arr1 ); // mango, lemon
?>
Arrays Flipping
• array array_flip ( array $trans ) :
Exchanges all keys with their associated values in an array
.
<?php
$arr1 = array(“mango" => 1, “banana" => 2);
$arr2 = array_flip($ arr1 );
var_dump($ arr2 ); // 1 => mango, 2=> banana
?>
Arrays Reversing
• array array_reverse ( array $array [, bool $preserve_keys
= false ] ) :
Return an array with elements in reverse order.
<?php
$arr1 = array(“mango" , “banana" , “tomato”);
$arr2 = array_reverse($ arr1 );
var_dump($ arr2 ); // tomato, banana, mango
?>
Merging Arrays
• array array_merge ( array $array1 [, array $array2 [, array $... ]] )
Merges the elements of one or more arrays together so
that the values of one are appended to the end of the
previous one. It returns the resulting array.
<?php
$array1 = array( 1, 2, 3 );
$array2 = array(4, 5, 6 );
$result = array_merge($array1, $array2);
print_r($result); // 1,2,3,4,5,6
?>
Array Sorting
• bool sort ( array &$array [, int $sort_flags =
SORT_REGULAR ] )
This function sorts an array. Elements will be arranged from lowest to
highest when this function has completed.
<?php
$fruits = array("lemon", "orange",
"banana", "apple");
sort($fruits);
var_dump($fruits); //apple, banana, lemon, orange
?>
Array Sorting
• bool rsort ( array &$array [, int $sort_flags = SORT_REGULAR ] )
This function sorts an array. Elements will be arranged from highest to
lowest when this function has completed.
<?php
$fruits = array("lemon", "orange",
"banana", "apple");
rsort($fruits);
var_dump($fruits); // orange, lemon, banana, apple
?>
Array Sorting
• bool asort ( array &$array [, int $sort_flags =
SORT_REGULAR ] )
Sorts an array from lowest to highest. This is used mainly when sorting
associative arrays.
<?php
$fruits = array( “one” => "lemon",
“two” => "orange",
“three” => "banana",
“four” => "apple");
asort($fruits);
var_dump($fruits); // four => apple, three => banana, one => lemon, two =>
orange
?>
Array Sorting
• bool arsort ( array &$array [, int $sort_flags =
SORT_REGULAR ] ) :
Sorts an array from highest to lowest. This is used mainly when sorting
associative arrays.
<?php
$fruits = array( “one” => "lemon",
“two” => "orange",
“three” => "banana",
“four” => "apple");
arsort($fruits);
var_dump($fruits); // two => orange, one => lemon, three =>
banana, four => apple
?>
Array Sorting
• bool ksort ( array &$array [, int $sort_flags = SORT_REGULAR ] )
Sorts an array by key, maintaining key to data correlations. This is
useful mainly for associative arrays.
<?php
$fruits = array("d"=>"lemon",
"a"=>"orange",
"b"=>"banana",
"c"=>"apple" );
ksort($fruits);
var_dump($fruits); // a => orange, b => banana, c => apple, d
=> lemon
?>
Array Sorting
• bool krsort ( array &$array [, int $sort_flags = SORT_REGULAR ] )
Sorts an array by key in reverse order, maintaining key to data
correlations. This is useful mainly for associative arrays.
<?php
$fruits = array( "d"=>"lemon",
"a"=>"orange",
"b"=>"banana",
"c"=>"apple" );
krsort($fruits);
var_dump($fruits); // d => lemon, c => apple, b => banana, a =>
orange
?>
Assignment
• Create a PHP function that takes an array as an argument and shows its
contents one on each line. This array may contain other arrays and these
arrays may contain others, etc. The function should display all the values
of these arrays. For example :
• If the array is like this :
$array = array(
1,
“Hello”,
array( 2, 3 ,4),
array(
5,
array( 6, 7, 8)
),
“No”
); ( Continued in the next slide )
Assignment
• The output should be like this :
1
Hello
2
3
4
5
6
7
8
No
What's Next?
• Strings.
Questions?

More Related Content

What's hot (15)

PPT
03 Php Array String Functions
Geshan Manandhar
 
PDF
4.1 PHP Arrays
Jalpesh Vasa
 
PPTX
PHP array 1
Mudasir Syed
 
PPTX
Chap 3php array part1
monikadeshmane
 
PDF
PHP 101
Muhammad Hijazi
 
PPTX
Array in php
ilakkiya
 
PDF
Scripting3
Nao Dara
 
PDF
Climbing the Abstract Syntax Tree (php[world] 2019)
James Titcumb
 
PPT
Arrays in php
Laiby Thomas
 
PPTX
PHP PPT FILE
AbhishekSharma2958
 
PPTX
07 php
CBRIARCSC
 
PDF
Wx::Perl::Smart
lichtkind
 
PDF
Laravel collections an overview - Laravel SP
Matheus Marabesi
 
PDF
Perl.Hacks.On.Vim
Lin Yo-An
 
PDF
Functional programming with php7
Sérgio Rafael Siqueira
 
03 Php Array String Functions
Geshan Manandhar
 
4.1 PHP Arrays
Jalpesh Vasa
 
PHP array 1
Mudasir Syed
 
Chap 3php array part1
monikadeshmane
 
PHP 101
Muhammad Hijazi
 
Array in php
ilakkiya
 
Scripting3
Nao Dara
 
Climbing the Abstract Syntax Tree (php[world] 2019)
James Titcumb
 
Arrays in php
Laiby Thomas
 
PHP PPT FILE
AbhishekSharma2958
 
07 php
CBRIARCSC
 
Wx::Perl::Smart
lichtkind
 
Laravel collections an overview - Laravel SP
Matheus Marabesi
 
Perl.Hacks.On.Vim
Lin Yo-An
 
Functional programming with php7
Sérgio Rafael Siqueira
 

Viewers also liked (20)

PPT
Class 7 - PHP Object Oriented Programming
Ahmed Swilam
 
PPTX
Class 8 - Database Programming
Ahmed Swilam
 
PPT
Class 3 - PHP Functions
Ahmed Swilam
 
PPT
Class 1 - World Wide Web Introduction
Ahmed Swilam
 
PPT
Class 2 - Introduction to PHP
Ahmed Swilam
 
PPT
Class 5 - PHP Strings
Ahmed Swilam
 
PPT
Class 6 - PHP Web Programming
Ahmed Swilam
 
PPT
Geek Austin PHP Class - Session 4
jimbojsb
 
DOC
S.G.Balaji Resume
Balaji Sg
 
KEY
PHPUnit testing to Zend_Test
Michelangelo van Dam
 
PDF
PHP MVC Tutorial 2
Yang Bruce
 
PDF
Intro To Mvc Development In Php
funkatron
 
PPT
Functions in php
Mudasir Syed
 
PDF
Making web forms using php
krishnapriya Tadepalli
 
PPTX
3 php forms
hello8421
 
PPTX
Why to choose laravel framework
Bo-Yi Wu
 
PPTX
How to choose web framework
Bo-Yi Wu
 
PPT
Class and Objects in PHP
Ramasubbu .P
 
PDF
Enterprise-Class PHP Security
ZendCon
 
PPTX
REST API Best Practices & Implementing in Codeigniter
Sachin G Kulkarni
 
Class 7 - PHP Object Oriented Programming
Ahmed Swilam
 
Class 8 - Database Programming
Ahmed Swilam
 
Class 3 - PHP Functions
Ahmed Swilam
 
Class 1 - World Wide Web Introduction
Ahmed Swilam
 
Class 2 - Introduction to PHP
Ahmed Swilam
 
Class 5 - PHP Strings
Ahmed Swilam
 
Class 6 - PHP Web Programming
Ahmed Swilam
 
Geek Austin PHP Class - Session 4
jimbojsb
 
S.G.Balaji Resume
Balaji Sg
 
PHPUnit testing to Zend_Test
Michelangelo van Dam
 
PHP MVC Tutorial 2
Yang Bruce
 
Intro To Mvc Development In Php
funkatron
 
Functions in php
Mudasir Syed
 
Making web forms using php
krishnapriya Tadepalli
 
3 php forms
hello8421
 
Why to choose laravel framework
Bo-Yi Wu
 
How to choose web framework
Bo-Yi Wu
 
Class and Objects in PHP
Ramasubbu .P
 
Enterprise-Class PHP Security
ZendCon
 
REST API Best Practices & Implementing in Codeigniter
Sachin G Kulkarni
 
Ad

Similar to Class 4 - PHP Arrays (20)

PPTX
Array Methods.pptx
stargaming38
 
PPTX
Array functions for all languages prog.pptx
Asmi309059
 
PPTX
Array functions using php programming language.pptx
NikhilVij6
 
PDF
Php tips-and-tricks4128
PrinceGuru MS
 
PDF
PHP and MySQL Tips and tricks, DC 2007
Damien Seguy
 
PPTX
UNIT IV (4).pptx
DrDhivyaaCRAssistant
 
PPTX
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
Dhivyaa C.R
 
PDF
PHP tips and tricks
Damien Seguy
 
PPTX
Chapter 2 wbp.pptx
40NehaPagariya
 
PPTX
PHP Array Functions.pptx
KirenKinu
 
KEY
Hidden treasures of Ruby
Tom Crinson
 
PPTX
Marcs (bio)perl course
BITS
 
ODP
Introduction to Perl - Day 1
Dave Cross
 
PPTX
Chap 3php array part4
monikadeshmane
 
PDF
Crafting Custom Interfaces with Sub::Exporter
Ricardo Signes
 
PDF
Adventures in Optimization
David Golden
 
PPTX
Chap 3php array part 3
monikadeshmane
 
KEY
Refactor like a boss
gsterndale
 
Array Methods.pptx
stargaming38
 
Array functions for all languages prog.pptx
Asmi309059
 
Array functions using php programming language.pptx
NikhilVij6
 
Php tips-and-tricks4128
PrinceGuru MS
 
PHP and MySQL Tips and tricks, DC 2007
Damien Seguy
 
UNIT IV (4).pptx
DrDhivyaaCRAssistant
 
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
Dhivyaa C.R
 
PHP tips and tricks
Damien Seguy
 
Chapter 2 wbp.pptx
40NehaPagariya
 
PHP Array Functions.pptx
KirenKinu
 
Hidden treasures of Ruby
Tom Crinson
 
Marcs (bio)perl course
BITS
 
Introduction to Perl - Day 1
Dave Cross
 
Chap 3php array part4
monikadeshmane
 
Crafting Custom Interfaces with Sub::Exporter
Ricardo Signes
 
Adventures in Optimization
David Golden
 
Chap 3php array part 3
monikadeshmane
 
Refactor like a boss
gsterndale
 
Ad

Recently uploaded (20)

PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PPTX
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
PDF
Staying Human in a Machine- Accelerated World
Catalin Jora
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PDF
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PDF
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
PDF
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
PDF
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
DOCX
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
PPTX
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
Staying Human in a Machine- Accelerated World
Catalin Jora
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 

Class 4 - PHP Arrays

  • 3. What are arrays ? • Arrays are data structures that contain a group of elements that are accessed through indexes. • Usage : <?php $x = array( “banana”, “orange”, “mango”, 3, 4 ); echo $x[0]; // banana echo $x[1]; // orange echo $x[3]; // 3 ?>
  • 4. Ways to work with arrays <?php $arr = array( “banana”, “orange”); $arr[4] = “mango”; $arr[] = “tomato”; var_dump($arr); ?>
  • 5. Printing Arrays • Use print_r() or var_dump() functions to output the contents of an array. <?php $arr = array( “banana”, “orange”); var_dump($arr ); // echoes the contents of the array ?>
  • 6. Enumerative VS. Associative • Enumerative arrays are indexed using only numerical indexes. <?php $arr = array( “banana”, “orange”); echo $arr[0]; // banana ?>
  • 7. Enumerative VS. Associative • Associative arrays allow the association of an arbitrary key to every element. <?php $arr = array( ‘first’ => “banana”, ‘second’ =>“orange”); echo $arr[‘first’]; // banana ?>
  • 8. Multidimensional Arrays • Multidimensional arrays are arrays that contain other arrays. <?php $arr = array( array( ‘burger’, 5, 15 ), array( ‘cola’, 2, 25 ), array( ‘Juice’, 3, 7 ), ); echo $arr[0][0]; // burger ?> Title Price Quantity Burger 5 15 Cola 2 25 Juice 3 7
  • 9. Array Iteration • foreach loop : Loop through arrays. <?php $arr1 = array(“mango" , “banana" , “tomato”); foreach( $arr1 as $key => $value ){ echo $key . “ ----- > “ . $value . “<br/>”; } ?>
  • 10. Array Iteration • We can use any other loop to go through arrays: <?php $arr1 = array(“mango" , “banana" , “tomato”); for( $i =0; $i < count($arr1) ; $i++ ){ echo $i . “ ----- > “ . $arr1 [$i] . “<br/>”; } ?>
  • 11. Class Exercise • Using arrays, write a PHP snippet that outputs the following table data in an HTML table and calculate the “total price” column values : Title Price Quantity Total Price Burger 5 10 Cola 2 4 Juice 3 7 Milk 2 6
  • 12. Class Exercise - Solution <?php $array = array( array( "name" => "Burger", "price" => 5, "quantity" => 10 ), array( "name" => "Cola", "price" => 2, "quantity" => 4 ), array( "name" => "Juice", "price" => 3, "quantity" => 7 ), array( "name" => "Milk", "price" => 2, "quantity" => 6 ) ); ( Continued in the next slide )
  • 13. Class Exercise - Solution echo '<table border="1">'; echo "<tr><th>Name</th><th>Price</th><th>Quantity</th><th>Total Price</th></tr>"; foreach( $array as $row ){ echo "<tr>"; echo "<td>" . $row['name'] . "</td>"; echo "<td>" . $row['price'] . "</td>"; echo "<td>" . $row['quantity'] . "</td>"; echo "<td>" . ( $row['price'] * $row['quantity'] ) . "</td>"; echo "</tr>"; } echo "</table>"; ?>
  • 14. Array Operations • PHP has a vast number of functions that allow us to do many operations on arrays. • For a complete reference of the functions, visit http://php.net/manual/en/ref.array.php.
  • 15. Array Comparison • Equality ‘==‘: True if the keys and values are equal. <?php $arr1 = array( “banana”, “mango”); $arr2 = array( “banana”, “tomato”); $arr3 = array( 1=> “mango”, 0=> “banana” ); if($arr1 == $arr2 ) // false if($arr1 == $arr3 ) // true ?>
  • 16. Array Comparison • Identical ‘===‘: True if the keys and values are equal and are in the same order. <?php $arr1 = array( “banana”, “mango”); $arr3 = array( 1=> “mango”, 0=> “banana” ); if($arr1 === $arr3 ) // false ?>
  • 17. Array Comparison • array array_diff ( array $array1 , array $array2 [, array $ ... ] ) : Returns an array containing all the entries from array1 that are not present in any of the other arrays. <?php $arr1 = array( “banana”, “mango”, “lemon”); $arr2 = array( “banana”, “mango”); $diff = array_diff($arr1, $arr2); // lemon ?>
  • 18. Counting Arrays • int count ( mixed $var [, int $mode = COUNT_NORMAL ] ) : Count all elements in an array. <?php $arr1 = array( “banana”, “mango”, “lemon”); echo count($arr1); // 3 ?>
  • 19. Searching Arrays • mixed array_search ( mixed $needle , array $haystack [, bool $strict ] ) : Searches the array for a given value and returns the corresponding key if successful. <?php $arr1 = array( “banana”, “mango”, “lemon”); echo array_search( 'mango', $arr1 ); // 1 echo array_search( ‘strawberry’, $arr1 ); // false ?>
  • 20. Deleting Items • void unset ( mixed $var [, mixed $var [, mixed $... ]] ): Unset a given variable. <?php $arr1 = array( “banana”, “mango”, “lemon”); unset( $arr1[0] ); var_dump($arr1 ); // mango, lemon ?>
  • 21. Arrays Flipping • array array_flip ( array $trans ) : Exchanges all keys with their associated values in an array . <?php $arr1 = array(“mango" => 1, “banana" => 2); $arr2 = array_flip($ arr1 ); var_dump($ arr2 ); // 1 => mango, 2=> banana ?>
  • 22. Arrays Reversing • array array_reverse ( array $array [, bool $preserve_keys = false ] ) : Return an array with elements in reverse order. <?php $arr1 = array(“mango" , “banana" , “tomato”); $arr2 = array_reverse($ arr1 ); var_dump($ arr2 ); // tomato, banana, mango ?>
  • 23. Merging Arrays • array array_merge ( array $array1 [, array $array2 [, array $... ]] ) Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array. <?php $array1 = array( 1, 2, 3 ); $array2 = array(4, 5, 6 ); $result = array_merge($array1, $array2); print_r($result); // 1,2,3,4,5,6 ?>
  • 24. Array Sorting • bool sort ( array &$array [, int $sort_flags = SORT_REGULAR ] ) This function sorts an array. Elements will be arranged from lowest to highest when this function has completed. <?php $fruits = array("lemon", "orange", "banana", "apple"); sort($fruits); var_dump($fruits); //apple, banana, lemon, orange ?>
  • 25. Array Sorting • bool rsort ( array &$array [, int $sort_flags = SORT_REGULAR ] ) This function sorts an array. Elements will be arranged from highest to lowest when this function has completed. <?php $fruits = array("lemon", "orange", "banana", "apple"); rsort($fruits); var_dump($fruits); // orange, lemon, banana, apple ?>
  • 26. Array Sorting • bool asort ( array &$array [, int $sort_flags = SORT_REGULAR ] ) Sorts an array from lowest to highest. This is used mainly when sorting associative arrays. <?php $fruits = array( “one” => "lemon", “two” => "orange", “three” => "banana", “four” => "apple"); asort($fruits); var_dump($fruits); // four => apple, three => banana, one => lemon, two => orange ?>
  • 27. Array Sorting • bool arsort ( array &$array [, int $sort_flags = SORT_REGULAR ] ) : Sorts an array from highest to lowest. This is used mainly when sorting associative arrays. <?php $fruits = array( “one” => "lemon", “two” => "orange", “three” => "banana", “four” => "apple"); arsort($fruits); var_dump($fruits); // two => orange, one => lemon, three => banana, four => apple ?>
  • 28. Array Sorting • bool ksort ( array &$array [, int $sort_flags = SORT_REGULAR ] ) Sorts an array by key, maintaining key to data correlations. This is useful mainly for associative arrays. <?php $fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple" ); ksort($fruits); var_dump($fruits); // a => orange, b => banana, c => apple, d => lemon ?>
  • 29. Array Sorting • bool krsort ( array &$array [, int $sort_flags = SORT_REGULAR ] ) Sorts an array by key in reverse order, maintaining key to data correlations. This is useful mainly for associative arrays. <?php $fruits = array( "d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple" ); krsort($fruits); var_dump($fruits); // d => lemon, c => apple, b => banana, a => orange ?>
  • 30. Assignment • Create a PHP function that takes an array as an argument and shows its contents one on each line. This array may contain other arrays and these arrays may contain others, etc. The function should display all the values of these arrays. For example : • If the array is like this : $array = array( 1, “Hello”, array( 2, 3 ,4), array( 5, array( 6, 7, 8) ), “No” ); ( Continued in the next slide )
  • 31. Assignment • The output should be like this : 1 Hello 2 3 4 5 6 7 8 No