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

PPT
Arrays in PHP
Compare Infobase Limited
 
PPT
Php Using Arrays
mussawir20
 
PDF
Php array
Nikul Shah
 
PDF
Arrays in PHP
Vineet Kumar Saini
 
PPTX
PHP Functions & Arrays
Henry Osborne
 
PPT
Php array
Core Lee
 
PDF
PHP Unit 4 arrays
Kumar
 
PDF
Sorting arrays in PHP
Vineet Kumar Saini
 
Php Using Arrays
mussawir20
 
Php array
Nikul Shah
 
Arrays in PHP
Vineet Kumar Saini
 
PHP Functions & Arrays
Henry Osborne
 
Php array
Core Lee
 
PHP Unit 4 arrays
Kumar
 
Sorting arrays in PHP
Vineet Kumar Saini
 

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
 
Ad

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
Chapter 2 wbp.pptx
40NehaPagariya
 
PPT
PHP - Introduction to PHP Arrays
Vibrant Technologies & Computers
 
PPTX
UNIT IV (4).pptx
DrDhivyaaCRAssistant
 
PPTX
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
Dhivyaa C.R
 
PPTX
Working with arrays in php
Kamal Acharya
 
PPTX
Array functions for all languages prog.pptx
Asmi309059
 
PPTX
Array functions using php programming language.pptx
NikhilVij6
 
PPT
Using arrays with PHP for forms and storing information
Nicole Ryan
 
PPTX
Array
hinanshu
 
PDF
PHP and MySQL Tips and tricks, DC 2007
Damien Seguy
 
PPT
PHP array 2
Mudasir Syed
 
PPTX
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
ZahouAmel1
 
PPTX
Unit 2-Arrays.pptx
mythili213835
 
PPTX
Array Methods.pptx
stargaming38
 
PPTX
Arrays in PHP
davidahaskins
 
PPTX
PHP Array Functions.pptx
KirenKinu
 
PDF
Php tips-and-tricks4128
PrinceGuru MS
 
PPT
arrays.ppt
SchoolEducationDepar
 
PPT
arrays.ppt
SchoolEducationDepar
 
Chapter 2 wbp.pptx
40NehaPagariya
 
PHP - Introduction to PHP Arrays
Vibrant Technologies & Computers
 
UNIT IV (4).pptx
DrDhivyaaCRAssistant
 
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
Dhivyaa C.R
 
Working with arrays in php
Kamal Acharya
 
Array functions for all languages prog.pptx
Asmi309059
 
Array functions using php programming language.pptx
NikhilVij6
 
Using arrays with PHP for forms and storing information
Nicole Ryan
 
Array
hinanshu
 
PHP and MySQL Tips and tricks, DC 2007
Damien Seguy
 
PHP array 2
Mudasir Syed
 
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
ZahouAmel1
 
Unit 2-Arrays.pptx
mythili213835
 
Array Methods.pptx
stargaming38
 
Arrays in PHP
davidahaskins
 
PHP Array Functions.pptx
KirenKinu
 
Php tips-and-tricks4128
PrinceGuru MS
 

Recently uploaded (20)

PPTX
Smart Infrastructure and Automation through IoT Sensors
Rejig Digital
 
PDF
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
 
PDF
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
PPTX
Stamford - Community User Group Leaders_ Agentblazer Status, AI Sustainabilit...
Amol Dixit
 
PDF
A Day in the Life of Location Data - Turning Where into How.pdf
Precisely
 
PPT
L2 Rules of Netiquette in Empowerment technology
Archibal2
 
PDF
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
PPTX
Coupa-Overview _Assumptions presentation
annapureddyn
 
PPTX
ChatGPT's Deck on The Enduring Legacy of Fax Machines
Greg Swan
 
PDF
Software Development Methodologies in 2025
KodekX
 
PPTX
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PPTX
How to Build a Scalable Micro-Investing Platform in 2025 - A Founder’s Guide ...
Third Rock Techkno
 
PDF
Best ERP System for Manufacturing in India | Elite Mindz
Elite Mindz
 
PDF
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
PDF
Revolutionize Operations with Intelligent IoT Monitoring and Control
Rejig Digital
 
PPTX
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
Francisco Vieira Júnior
 
PDF
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
PDF
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
Smart Infrastructure and Automation through IoT Sensors
Rejig Digital
 
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
 
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
Stamford - Community User Group Leaders_ Agentblazer Status, AI Sustainabilit...
Amol Dixit
 
A Day in the Life of Location Data - Turning Where into How.pdf
Precisely
 
L2 Rules of Netiquette in Empowerment technology
Archibal2
 
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
Coupa-Overview _Assumptions presentation
annapureddyn
 
ChatGPT's Deck on The Enduring Legacy of Fax Machines
Greg Swan
 
Software Development Methodologies in 2025
KodekX
 
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
How to Build a Scalable Micro-Investing Platform in 2025 - A Founder’s Guide ...
Third Rock Techkno
 
Best ERP System for Manufacturing in India | Elite Mindz
Elite Mindz
 
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
Revolutionize Operations with Intelligent IoT Monitoring and Control
Rejig Digital
 
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
Francisco Vieira Júnior
 
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 

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