SlideShare a Scribd company logo
Chapter 3
PHP
Array part-3
1
Monica Deshmane(H.V.Desai college,Pune)
Topics
• Converting between arrays and variables
• Traversing arrays
• Iterator function
• Reducing an array
• flipping an array
• Array shuffle
• Acting on entire array
• Using arrays(set,stack,queue)
Monica Deshmane(H.V.Desai college,Pune) 2
Conversion
between
array and variable
Monica Deshmane(H.V.Desai college,Pune) 3
1. Creating variables from array
Which parameters required?
?
extract($arr, [ EXTR_PREFIXtype, prefix]);
Type can be -
EXTR_PREFIX_ALL
Third parameter is prefix for all variables
Can we extract indexed array?
No..
Monica Deshmane(H.V.Desai college,Pune) 4
Example
$arr = array('name'=>'C', 'author'=>'Dennis Richie',
'price'=>500);
extract($arr, EXTR_PREFIX_ALL, "book");
echo $book_name." ".$book_author." ".$book_price;
//C Dennis Richie 500
Monica Deshmane(H.V.Desai college,Pune) 5
2. Creating array from variables
Which parameters required?
?
List of variables
$arr = compact(var1,var2,…);
print_r($a);
Monica Deshmane(H.V.Desai college,Pune) 6
Example
$name = ' C';
$author='Dennis';
$price =500;
$a = compact('name', 'author', 'price');
print_r($a);
$arr = array('name', 'author', 'price');
$a=compact($arr);
echo "<br>";
print_r($a);
//Array ( [name] => C [author] => Dennis [price] => 500 )
//Array ( [name] => C [author] => Dennis [price] => 500 )
Monica Deshmane(H.V.Desai college,Pune) 7
Travesing arrays / locate elements in array
foreach -
$sub = array('C', 'CPP', 'Java', 'PHP', 'DS');
foreach($sub as $value)
{ echo $value. " ";
}
C CPP Java PHP DS
$book = array('C' => 'Richie' , 'CPP' => 'Bjarne', 'Java'
=> 'Sun', 'PHP'=>'Orelly');
foreach($book as $key=>$value)
{ echo $key." is ".$value." ";
}
C is Richie CPP is Bjarne Java is Sun PHP is Orelly
Monica Deshmane(H.V.Desai college,Pune) 8
Iterator function
current( ) - Returns the currently pointed element 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.
each( ) - Returns the key and value of the current element
as an array and moves the iterator to the next element in
the array.
key( ) - Returns the key of the current element.
Monica Deshmane(H.V.Desai college,Pune) 9
Iterator function
$book = array('C' => 'Richie' , 'CPP' => 'Bjarne', 'Java'
=> 'Sun', 'PHP'=>'Orelly');
reset($book);
while(list($key, $value) = each($book))
{
echo $keys.” is “.$value;
}
Monica Deshmane(H.V.Desai college,Pune) 10
Calling a Function for Each Array Element
function print_row($value, $key)
{
echo $key." ".$value."<br>";
}
$book = array('C' => 'Richie' , 'CPP' => 'Bjarne', 'Java'
=> 'Sun', 'PHP'=>'Orelly');
array_walk($book, 'print_row');
C Richie
CPP Bjarne
Java Sun
PHP Orelly
Monica Deshmane(H.V.Desai college,Pune) 11
Question-
Which function returns all parameters passed to
function as an array?
Array_walk()
Monica Deshmane(H.V.Desai college,Pune) 12
Iterator function
Using for loop
$sub = array('C', 'CPP', 'Java', 'PHP', 'DS');
for($i = 0; $i < count($sub); $i++)
{
$value = $sub[$i];
echo "$valuen";
}
C CPP Java PHP DS
Monica Deshmane(H.V.Desai college,Pune) 13
Reducing an array
array_reduce: This also calls function on each element. It
returns single value.
Which parameters required?
?
$result = array_reduce(array, function_name [, default ]);
Default value is start value.
Monica Deshmane(H.V.Desai college,Pune) 14
Reducing an array
array_reduce: This also calls function on each element. It
returns single value.
$result = array_reduce(array, function_name [, default ]);
function add($v1,$v2)
{
return $v1+$v2;
}
$a=array(10,15,20);
print_r(array_reduce($a,"add"));
add(0, 10);
add(10, 15);
add(25, 20); Output: 45
Monica Deshmane(H.V.Desai college,Pune) 15
Reducing an array
We can pass initial value as third parameter
function add($v1,$v2)
{
return $v1+$v2;
}
$a=array(10,15,20);
print_r(array_reduce($a,"add", 5));
add(5, 10);
add(15, 15);
add(30, 20);
Output: 50
Monica Deshmane(H.V.Desai college,Pune) 16
Reversing an array
array_reverse(array,preserve)
preserve(optional) Specifies if the function should
preserve the array's keys or not.(true, false)
$book = array('C' => 'Richie' , 'CPP' => 'Bjarne', 'Java'
=> 'Sun', 'PHP'=>'Orelly');
$rev = array_reverse($book);
print_r($rev);
//Array ( [PHP] => Orelly [Java] => Sun [CPP] =>
Bjarne [C] => Richie )
Monica Deshmane(H.V.Desai college,Pune) 17
flipping an array
array_flip(array) - returns an array with all the
original keys as values, and all original values as
keys.
$book = array('C' => 'Richie' , 'CPP' => 'Bjarne', 'Java'
=> 'Sun', 'PHP'=>'Orelly');
$rev = array_flip($book);
print_r($rev);
//Array ( [Richie] => C [Bjarne] => CPP [Sun] =>
Java [Orelly] => PHP )
Monica Deshmane(H.V.Desai college,Pune) 18
Array shuffle
Jumbles the elements of array.
i.e. elements in array comes in random order.
Syntax:-
Shuffle(array1);
$arr=array(1,3,6,8);
print_r($arr);
shuffle($arr);
print_r($arr);
shuffle($arr);
print_r($arr);
//elements are shuffled in same array itself.
Monica Deshmane(H.V.Desai college,Pune) 19
Acting on entire array
Array_sum
Array_diff
Array_merge
Array_filter
Set-
array_intersect
array_merge
array_diff
stack=-array_push
array_pop
Queue-
array_unshift
array_shift
Monica Deshmane(H.V.Desai college,Pune)
20
Acting on entire array
Calculating the Sum of an Array
array_sum( ) - function adds up the values in an
indexed or associative array.
$sum = array_sum(array);
$marks = array(25, 37, 48, 90);
$total = array_sum($marks);
echo $total;
//200
Monica Deshmane(H.V.Desai college,Pune)
21
Acting on entire array
Merging two arrays
array_merge( ) - function intelligently merges two or
more arrays.
$merged = array_merge(array1, array2 [, array ... ])
$book1 = array('C' => 'Richie' , 'CPP' => 'Bjarne',
'Java' => 'Sun');
$book2 = array('C' => 'Dennis' , 'SDK' => 'Petzold',
'PHP'=>'Orelly');
$merged = array_merge($book1, $book2);
print_r($merged);
//Array ( [C] => Dennis [CPP] => Bjarne [Java] =>
Sun [SDK] => Petzold [PHP] => Orelly )
note-we can merge indexed and associative array
Monica Deshmane(H.V.Desai college,Pune) 22
Acting on entire array
Difference between two arrays
array_diff( ) - function identifies values from one
array that are not present in others.
$diff = array_diff(array1, array2 [, array ... ]);
$book1 = array('C' => 'Richie' , 'CPP' => 'Bjarne',
'Java' => 'Sun');
$book2 = array('C' => 'Dennis' , 'SDK' => 'Petzold',
'PHP'=>'Orelly', 'XML' => 'Wrox');
$book3 = array('XML' => 'Wrox' , 'CPP' => 'Bjarne',
'Unix' => 'Bach', 'SDK'=>'Petzold');
$diff = array_diff($book1, $book2, $book3);
print_r($diff);
Monica Deshmane(H.V.Desai college,Pune) 23
Acting on entire array
//Array ( [C] => Richie [Java] => Sun )
//also see example of indexed array.
Monica Deshmane(H.V.Desai college,Pune)
24
Acting on entire array
Filtering Elements from an Array
array_filter( ) - function is used to identify a subset of
an array based on its values.
Used to return subset according to function.
$filtered = array_filter(array, callback);
Each value of array is passed to the function named in
callback. The returned array contains only those
elements of the original array for which the function
returns a true value.
Monica Deshmane(H.V.Desai college,Pune) 25
Acting on entire array
Filtering Elements from an Array
function myfunction($v)
{
if ($v==="Blue")
{
return true;
}
return false;
}
$a=array(0=>"Red",1=>"Blue",2=>"Green");
print_r(array_filter($a,"myfunction"));
//Array ( [1] => Blue )
Monica Deshmane(H.V.Desai college,Pune) 26
Ex.2)
Function even($v)
{
if $v%2==0
return 1;
else
return 0;
}
$n=array(4,7,2,10,9);
print_r(array_filter($a,”even”));
Monica Deshmane(H.V.Desai college,Pune) 27
Using arrays – for sets
1. Union - The union of two sets is all the elements
from both sets, with duplicates removed. The
array_merge( ) and array_unique( ) functions let
you calculate the union.
2. Intersection - array_intersect( ) function takes any
number of arrays as arguments and returns an
array of those values that exist in each.
3. Difference - array_diff( ) function calculates this,
returning an array with values from the first array
that are not present in the second.
Monica Deshmane(H.V.Desai college,Pune) 28
Using arrays – for stacks
For growing or shrinking array stack /queue is used.
Stacks –LIFO
We can create stacks using a pair of PHP functions,
array_push( ) and array_pop( ).
1)array_push(array,val1,[val2,….]);
We can push 1 or more elements on top of stack.
$a=array(1,2);
array_push($a,5,4,3);
2)array_pop(array);
We can pop only 1 element from top of stack.
//consider above array
array_pop($a);
Monica Deshmane(H.V.Desai college,Pune) 29
Using arrays – for Queue
For growing or shrinking array stack /queue is used.
Queue –FIFO
We can create queue using a pair of PHP functions,
array_shift( ) and array_unshift( ).
1)array_unshift(array,val1,[val2,….]);
We can insert 1 or more elements in front of queue.
$a=array(1,2);
array_unshift($a,5,4,3);//see the order
2)Remainingarr=array_shift(array);
We can delete only 1 element from front of queue.
//consider above array
$rem=array_shift($a);
Monica Deshmane(H.V.Desai college,Pune) 30

More Related Content

PPTX
Chap 3php array part4
monikadeshmane
 
PPTX
Chap 3php array part 2
monikadeshmane
 
PPTX
Switching from java to groovy
Paul Woods
 
PPT
Class 4 - PHP Arrays
Ahmed Swilam
 
PDF
Arrays in PHP
Vineet Kumar Saini
 
PDF
PHP Unit 4 arrays
Kumar
 
DOCX
List of all php array functions
Chetan Patel
 
PPTX
Chap 3php array part1
monikadeshmane
 
Chap 3php array part4
monikadeshmane
 
Chap 3php array part 2
monikadeshmane
 
Switching from java to groovy
Paul Woods
 
Class 4 - PHP Arrays
Ahmed Swilam
 
Arrays in PHP
Vineet Kumar Saini
 
PHP Unit 4 arrays
Kumar
 
List of all php array functions
Chetan Patel
 
Chap 3php array part1
monikadeshmane
 

What's hot (20)

PDF
Forget about loops
Dušan Kasan
 
PDF
Php array
Nikul Shah
 
PPTX
PHP Functions & Arrays
Henry Osborne
 
PDF
4.1 PHP Arrays
Jalpesh Vasa
 
PDF
The underestimated power of KeyPaths
Vincent Pradeilles
 
PDF
WordCamp Portland 2018: PHP for WordPress
Alena Holligan
 
PDF
Iterators, ArrayAccess & Countable (Oh My!) - Madison PHP 2014
Sandy Smith
 
PPTX
PHP array 1
Mudasir Syed
 
PPT
Php Using Arrays
mussawir20
 
PPT
Php array
Core Lee
 
PDF
Codeware
Uri Nativ
 
PPT
Class 5 - PHP Strings
Ahmed Swilam
 
PDF
Python - Lecture 3
Ravi Kiran Khareedi
 
PPT
Arrays in PHP
Compare Infobase Limited
 
PPTX
PHP Strings and Patterns
Henry Osborne
 
PDF
Python programming : List and tuples
Emertxe Information Technologies Pvt Ltd
 
PDF
7 Habits For a More Functional Swift
Jason Larsen
 
PDF
Your code sucks, let's fix it
Rafael Dohms
 
DOCX
PERL for QA - Important Commands and applications
Sunil Kumar Gunasekaran
 
Forget about loops
Dušan Kasan
 
Php array
Nikul Shah
 
PHP Functions & Arrays
Henry Osborne
 
4.1 PHP Arrays
Jalpesh Vasa
 
The underestimated power of KeyPaths
Vincent Pradeilles
 
WordCamp Portland 2018: PHP for WordPress
Alena Holligan
 
Iterators, ArrayAccess & Countable (Oh My!) - Madison PHP 2014
Sandy Smith
 
PHP array 1
Mudasir Syed
 
Php Using Arrays
mussawir20
 
Php array
Core Lee
 
Codeware
Uri Nativ
 
Class 5 - PHP Strings
Ahmed Swilam
 
Python - Lecture 3
Ravi Kiran Khareedi
 
PHP Strings and Patterns
Henry Osborne
 
Python programming : List and tuples
Emertxe Information Technologies Pvt Ltd
 
7 Habits For a More Functional Swift
Jason Larsen
 
Your code sucks, let's fix it
Rafael Dohms
 
PERL for QA - Important Commands and applications
Sunil Kumar Gunasekaran
 
Ad

Similar to Chap 3php array part 3 (20)

PPT
Php Chapter 2 3 Training
Chris Chubb
 
PPTX
Array functions for all languages prog.pptx
Asmi309059
 
PPTX
Array functions using php programming language.pptx
NikhilVij6
 
PPTX
Chapter 2 wbp.pptx
40NehaPagariya
 
PPT
Arrays in php
Laiby Thomas
 
PPTX
Php & my sql
Norhisyam Dasuki
 
PDF
PHP Conference Asia 2016
Britta Alex
 
PPT
9780538745840 ppt ch06
Terry Yoast
 
PPTX
The groovy puzzlers (as Presented at JavaOne 2014)
GroovyPuzzlers
 
PPTX
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
Dhivyaa C.R
 
PPTX
UNIT IV (4).pptx
DrDhivyaaCRAssistant
 
PPTX
java script
monikadeshmane
 
PPTX
php string part 3
monikadeshmane
 
PPTX
Marcs (bio)perl course
BITS
 
PDF
PHP for Python Developers
Carlos Vences
 
PDF
Ms Ajax Array Extensions
jason hu 金良胡
 
PPTX
Unit 2-Arrays.pptx
mythili213835
 
PPTX
Php functions
JIGAR MAKHIJA
 
DOCX
PHP record- with all programs and output
KavithaK23
 
PPTX
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
ZahouAmel1
 
Php Chapter 2 3 Training
Chris Chubb
 
Array functions for all languages prog.pptx
Asmi309059
 
Array functions using php programming language.pptx
NikhilVij6
 
Chapter 2 wbp.pptx
40NehaPagariya
 
Arrays in php
Laiby Thomas
 
Php & my sql
Norhisyam Dasuki
 
PHP Conference Asia 2016
Britta Alex
 
9780538745840 ppt ch06
Terry Yoast
 
The groovy puzzlers (as Presented at JavaOne 2014)
GroovyPuzzlers
 
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
Dhivyaa C.R
 
UNIT IV (4).pptx
DrDhivyaaCRAssistant
 
java script
monikadeshmane
 
php string part 3
monikadeshmane
 
Marcs (bio)perl course
BITS
 
PHP for Python Developers
Carlos Vences
 
Ms Ajax Array Extensions
jason hu 金良胡
 
Unit 2-Arrays.pptx
mythili213835
 
Php functions
JIGAR MAKHIJA
 
PHP record- with all programs and output
KavithaK23
 
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
ZahouAmel1
 
Ad

More from monikadeshmane (16)

PPTX
File system node js
monikadeshmane
 
PPTX
Nodejs functions & modules
monikadeshmane
 
PPTX
Nodejs buffers
monikadeshmane
 
PPTX
Intsllation & 1st program nodejs
monikadeshmane
 
PPTX
Nodejs basics
monikadeshmane
 
PPTX
Chap 5 php files part-2
monikadeshmane
 
PPTX
Chap 5 php files part 1
monikadeshmane
 
PPTX
Chap4 oop class (php) part 2
monikadeshmane
 
PPTX
Chap4 oop class (php) part 1
monikadeshmane
 
PPTX
PHP function
monikadeshmane
 
PPTX
php string part 4
monikadeshmane
 
PPTX
php string-part 2
monikadeshmane
 
PPTX
PHP string-part 1
monikadeshmane
 
PPT
ip1clientserver model
monikadeshmane
 
PPTX
Chap1introppt2php(finally done)
monikadeshmane
 
PPTX
Chap1introppt1php basic
monikadeshmane
 
File system node js
monikadeshmane
 
Nodejs functions & modules
monikadeshmane
 
Nodejs buffers
monikadeshmane
 
Intsllation & 1st program nodejs
monikadeshmane
 
Nodejs basics
monikadeshmane
 
Chap 5 php files part-2
monikadeshmane
 
Chap 5 php files part 1
monikadeshmane
 
Chap4 oop class (php) part 2
monikadeshmane
 
Chap4 oop class (php) part 1
monikadeshmane
 
PHP function
monikadeshmane
 
php string part 4
monikadeshmane
 
php string-part 2
monikadeshmane
 
PHP string-part 1
monikadeshmane
 
ip1clientserver model
monikadeshmane
 
Chap1introppt2php(finally done)
monikadeshmane
 
Chap1introppt1php basic
monikadeshmane
 

Recently uploaded (20)

DOCX
Action Plan_ARAL PROGRAM_ STAND ALONE SHS.docx
Levenmartlacuna1
 
PPTX
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
PPTX
PREVENTIVE PEDIATRIC. pptx
AneetaSharma15
 
PDF
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
AshifaRamadhani
 
PPTX
Care of patients with elImination deviation.pptx
AneetaSharma15
 
PDF
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
Sandeep Swamy
 
PPTX
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
DOCX
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
PDF
The Picture of Dorian Gray summary and depiction
opaliyahemel
 
PPTX
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
PDF
5.EXPLORING-FORCES-Detailed-Notes.pdf/8TH CLASS SCIENCE CURIOSITY
Sandeep Swamy
 
PDF
Landforms and landscapes data surprise preview
jpinnuck
 
PDF
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
PDF
Sunset Boulevard Student Revision Booklet
jpinnuck
 
PPTX
NOI Hackathon - Summer Edition - GreenThumber.pptx
MartinaBurlando1
 
PPTX
Understanding operators in c language.pptx
auteharshil95
 
PDF
7.Particulate-Nature-of-Matter.ppt/8th class science curiosity/by k sandeep s...
Sandeep Swamy
 
PPTX
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
academysrusti114
 
PPTX
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
PDF
Virat Kohli- the Pride of Indian cricket
kushpar147
 
Action Plan_ARAL PROGRAM_ STAND ALONE SHS.docx
Levenmartlacuna1
 
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
PREVENTIVE PEDIATRIC. pptx
AneetaSharma15
 
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
AshifaRamadhani
 
Care of patients with elImination deviation.pptx
AneetaSharma15
 
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
Sandeep Swamy
 
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
The Picture of Dorian Gray summary and depiction
opaliyahemel
 
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
5.EXPLORING-FORCES-Detailed-Notes.pdf/8TH CLASS SCIENCE CURIOSITY
Sandeep Swamy
 
Landforms and landscapes data surprise preview
jpinnuck
 
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
Sunset Boulevard Student Revision Booklet
jpinnuck
 
NOI Hackathon - Summer Edition - GreenThumber.pptx
MartinaBurlando1
 
Understanding operators in c language.pptx
auteharshil95
 
7.Particulate-Nature-of-Matter.ppt/8th class science curiosity/by k sandeep s...
Sandeep Swamy
 
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
academysrusti114
 
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
Virat Kohli- the Pride of Indian cricket
kushpar147
 

Chap 3php array part 3

  • 1. Chapter 3 PHP Array part-3 1 Monica Deshmane(H.V.Desai college,Pune)
  • 2. Topics • Converting between arrays and variables • Traversing arrays • Iterator function • Reducing an array • flipping an array • Array shuffle • Acting on entire array • Using arrays(set,stack,queue) Monica Deshmane(H.V.Desai college,Pune) 2
  • 3. Conversion between array and variable Monica Deshmane(H.V.Desai college,Pune) 3
  • 4. 1. Creating variables from array Which parameters required? ? extract($arr, [ EXTR_PREFIXtype, prefix]); Type can be - EXTR_PREFIX_ALL Third parameter is prefix for all variables Can we extract indexed array? No.. Monica Deshmane(H.V.Desai college,Pune) 4
  • 5. Example $arr = array('name'=>'C', 'author'=>'Dennis Richie', 'price'=>500); extract($arr, EXTR_PREFIX_ALL, "book"); echo $book_name." ".$book_author." ".$book_price; //C Dennis Richie 500 Monica Deshmane(H.V.Desai college,Pune) 5
  • 6. 2. Creating array from variables Which parameters required? ? List of variables $arr = compact(var1,var2,…); print_r($a); Monica Deshmane(H.V.Desai college,Pune) 6
  • 7. Example $name = ' C'; $author='Dennis'; $price =500; $a = compact('name', 'author', 'price'); print_r($a); $arr = array('name', 'author', 'price'); $a=compact($arr); echo "<br>"; print_r($a); //Array ( [name] => C [author] => Dennis [price] => 500 ) //Array ( [name] => C [author] => Dennis [price] => 500 ) Monica Deshmane(H.V.Desai college,Pune) 7
  • 8. Travesing arrays / locate elements in array foreach - $sub = array('C', 'CPP', 'Java', 'PHP', 'DS'); foreach($sub as $value) { echo $value. " "; } C CPP Java PHP DS $book = array('C' => 'Richie' , 'CPP' => 'Bjarne', 'Java' => 'Sun', 'PHP'=>'Orelly'); foreach($book as $key=>$value) { echo $key." is ".$value." "; } C is Richie CPP is Bjarne Java is Sun PHP is Orelly Monica Deshmane(H.V.Desai college,Pune) 8
  • 9. Iterator function current( ) - Returns the currently pointed element 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. each( ) - Returns the key and value of the current element as an array and moves the iterator to the next element in the array. key( ) - Returns the key of the current element. Monica Deshmane(H.V.Desai college,Pune) 9
  • 10. Iterator function $book = array('C' => 'Richie' , 'CPP' => 'Bjarne', 'Java' => 'Sun', 'PHP'=>'Orelly'); reset($book); while(list($key, $value) = each($book)) { echo $keys.” is “.$value; } Monica Deshmane(H.V.Desai college,Pune) 10
  • 11. Calling a Function for Each Array Element function print_row($value, $key) { echo $key." ".$value."<br>"; } $book = array('C' => 'Richie' , 'CPP' => 'Bjarne', 'Java' => 'Sun', 'PHP'=>'Orelly'); array_walk($book, 'print_row'); C Richie CPP Bjarne Java Sun PHP Orelly Monica Deshmane(H.V.Desai college,Pune) 11
  • 12. Question- Which function returns all parameters passed to function as an array? Array_walk() Monica Deshmane(H.V.Desai college,Pune) 12
  • 13. Iterator function Using for loop $sub = array('C', 'CPP', 'Java', 'PHP', 'DS'); for($i = 0; $i < count($sub); $i++) { $value = $sub[$i]; echo "$valuen"; } C CPP Java PHP DS Monica Deshmane(H.V.Desai college,Pune) 13
  • 14. Reducing an array array_reduce: This also calls function on each element. It returns single value. Which parameters required? ? $result = array_reduce(array, function_name [, default ]); Default value is start value. Monica Deshmane(H.V.Desai college,Pune) 14
  • 15. Reducing an array array_reduce: This also calls function on each element. It returns single value. $result = array_reduce(array, function_name [, default ]); function add($v1,$v2) { return $v1+$v2; } $a=array(10,15,20); print_r(array_reduce($a,"add")); add(0, 10); add(10, 15); add(25, 20); Output: 45 Monica Deshmane(H.V.Desai college,Pune) 15
  • 16. Reducing an array We can pass initial value as third parameter function add($v1,$v2) { return $v1+$v2; } $a=array(10,15,20); print_r(array_reduce($a,"add", 5)); add(5, 10); add(15, 15); add(30, 20); Output: 50 Monica Deshmane(H.V.Desai college,Pune) 16
  • 17. Reversing an array array_reverse(array,preserve) preserve(optional) Specifies if the function should preserve the array's keys or not.(true, false) $book = array('C' => 'Richie' , 'CPP' => 'Bjarne', 'Java' => 'Sun', 'PHP'=>'Orelly'); $rev = array_reverse($book); print_r($rev); //Array ( [PHP] => Orelly [Java] => Sun [CPP] => Bjarne [C] => Richie ) Monica Deshmane(H.V.Desai college,Pune) 17
  • 18. flipping an array array_flip(array) - returns an array with all the original keys as values, and all original values as keys. $book = array('C' => 'Richie' , 'CPP' => 'Bjarne', 'Java' => 'Sun', 'PHP'=>'Orelly'); $rev = array_flip($book); print_r($rev); //Array ( [Richie] => C [Bjarne] => CPP [Sun] => Java [Orelly] => PHP ) Monica Deshmane(H.V.Desai college,Pune) 18
  • 19. Array shuffle Jumbles the elements of array. i.e. elements in array comes in random order. Syntax:- Shuffle(array1); $arr=array(1,3,6,8); print_r($arr); shuffle($arr); print_r($arr); shuffle($arr); print_r($arr); //elements are shuffled in same array itself. Monica Deshmane(H.V.Desai college,Pune) 19
  • 20. Acting on entire array Array_sum Array_diff Array_merge Array_filter Set- array_intersect array_merge array_diff stack=-array_push array_pop Queue- array_unshift array_shift Monica Deshmane(H.V.Desai college,Pune) 20
  • 21. Acting on entire array Calculating the Sum of an Array array_sum( ) - function adds up the values in an indexed or associative array. $sum = array_sum(array); $marks = array(25, 37, 48, 90); $total = array_sum($marks); echo $total; //200 Monica Deshmane(H.V.Desai college,Pune) 21
  • 22. Acting on entire array Merging two arrays array_merge( ) - function intelligently merges two or more arrays. $merged = array_merge(array1, array2 [, array ... ]) $book1 = array('C' => 'Richie' , 'CPP' => 'Bjarne', 'Java' => 'Sun'); $book2 = array('C' => 'Dennis' , 'SDK' => 'Petzold', 'PHP'=>'Orelly'); $merged = array_merge($book1, $book2); print_r($merged); //Array ( [C] => Dennis [CPP] => Bjarne [Java] => Sun [SDK] => Petzold [PHP] => Orelly ) note-we can merge indexed and associative array Monica Deshmane(H.V.Desai college,Pune) 22
  • 23. Acting on entire array Difference between two arrays array_diff( ) - function identifies values from one array that are not present in others. $diff = array_diff(array1, array2 [, array ... ]); $book1 = array('C' => 'Richie' , 'CPP' => 'Bjarne', 'Java' => 'Sun'); $book2 = array('C' => 'Dennis' , 'SDK' => 'Petzold', 'PHP'=>'Orelly', 'XML' => 'Wrox'); $book3 = array('XML' => 'Wrox' , 'CPP' => 'Bjarne', 'Unix' => 'Bach', 'SDK'=>'Petzold'); $diff = array_diff($book1, $book2, $book3); print_r($diff); Monica Deshmane(H.V.Desai college,Pune) 23
  • 24. Acting on entire array //Array ( [C] => Richie [Java] => Sun ) //also see example of indexed array. Monica Deshmane(H.V.Desai college,Pune) 24
  • 25. Acting on entire array Filtering Elements from an Array array_filter( ) - function is used to identify a subset of an array based on its values. Used to return subset according to function. $filtered = array_filter(array, callback); Each value of array is passed to the function named in callback. The returned array contains only those elements of the original array for which the function returns a true value. Monica Deshmane(H.V.Desai college,Pune) 25
  • 26. Acting on entire array Filtering Elements from an Array function myfunction($v) { if ($v==="Blue") { return true; } return false; } $a=array(0=>"Red",1=>"Blue",2=>"Green"); print_r(array_filter($a,"myfunction")); //Array ( [1] => Blue ) Monica Deshmane(H.V.Desai college,Pune) 26
  • 27. Ex.2) Function even($v) { if $v%2==0 return 1; else return 0; } $n=array(4,7,2,10,9); print_r(array_filter($a,”even”)); Monica Deshmane(H.V.Desai college,Pune) 27
  • 28. Using arrays – for sets 1. Union - The union of two sets is all the elements from both sets, with duplicates removed. The array_merge( ) and array_unique( ) functions let you calculate the union. 2. Intersection - array_intersect( ) function takes any number of arrays as arguments and returns an array of those values that exist in each. 3. Difference - array_diff( ) function calculates this, returning an array with values from the first array that are not present in the second. Monica Deshmane(H.V.Desai college,Pune) 28
  • 29. Using arrays – for stacks For growing or shrinking array stack /queue is used. Stacks –LIFO We can create stacks using a pair of PHP functions, array_push( ) and array_pop( ). 1)array_push(array,val1,[val2,….]); We can push 1 or more elements on top of stack. $a=array(1,2); array_push($a,5,4,3); 2)array_pop(array); We can pop only 1 element from top of stack. //consider above array array_pop($a); Monica Deshmane(H.V.Desai college,Pune) 29
  • 30. Using arrays – for Queue For growing or shrinking array stack /queue is used. Queue –FIFO We can create queue using a pair of PHP functions, array_shift( ) and array_unshift( ). 1)array_unshift(array,val1,[val2,….]); We can insert 1 or more elements in front of queue. $a=array(1,2); array_unshift($a,5,4,3);//see the order 2)Remainingarr=array_shift(array); We can delete only 1 element from front of queue. //consider above array $rem=array_shift($a); Monica Deshmane(H.V.Desai college,Pune) 30