SlideShare a Scribd company logo
Chapter 3
PHP
Array-part 2
1
Monica Deshmane(H.V.Desai college,Pune)
Topics
• Extracting multiple values
• Slicing an array
• Splitting of an array into chunks
• Retrieving keys & values
• Searching key or values
• splice
Monica Deshmane(H.V.Desai college,Pune) 2
Revision..
Monica Deshmane(H.V.Desai college,Pune) 3
Introduction
•An array is a special variable, which can hold
more than one value at a time.
•It means it is continuous block of memory
locations.
•Arrays can be categorised using dimensions.
•1-D array
•2-D Array
•Multidimenstional array.
•In each category we have 2 types of arrays-
•Indexed Array
•Associative Array
Monica Deshmane(H.V.Desai college,Pune) 4
Indexed Vs Associative Array
Indexed Arrays:
The keys of an indexed array are integers,
beginning at 0.
Indexed arrays are used when you identify
things by their position.
Associative arrays:
This array contains strings as keys and behave more
like two-column tables.
The first column is the key, which is used to access
the value.
PHP internally stores all arrays as associative arrays.
Monica Deshmane(H.V.Desai college,Pune) 5
Extracting multiple values
OR multiple variable assignment
To copy all of an array's values into
variables, use the list( ) construct:
Which parameters required?
?
array
list($variable, ...) = $array;
Monica Deshmane(H.V.Desai college,Pune) 6
Extracting multiple values/ multiple variable assignment
$book = array('C', 'Dennis Richie', 500);
list($n, $a, $p) = $book;
echo "Values : ".$n." ".$a." ".$p;
//Values : C Dennis Richie 500
$book = array('C', 'Dennis Richie', 500);
list($n, $a) = $book;
echo "<br> Values : ".$n." ".$a." ";
//Values : C Dennis Richie
Monica Deshmane(H.V.Desai college,Pune) 7
Extracting multiple values
$inventor= array('C', 'Dennis Richie');
list($n, $a, $p) = $inventor;
echo "<br> Values : ".$n." ".$a." ".$p;
//Values : C Dennis Richie NULL
$values = range('a', 'e');
list($m,,$n,,$o) = $values; //,, for alternate elements
echo "<br> Values : ".$m." ".$n." ".$o;
//Values : a c e
Monica Deshmane(H.V.Desai college,Pune) 8
Slicing an array
To retrieve sub part of array
Which parameters required?
?
Array
Start index
No. of locations
$subset = array_slice(array, offset,[ length]);
Monica Deshmane(H.V.Desai college,Pune) 9
Slicing an array
$sub = array('C', 'CPP', 'Java', 'PHP', 'DS');
$middle = array_slice($sub, 2, 3);
print_r($middle);
//Array ( [0] => Java [1] => PHP [2] => DS )
$book = array('C' => 'Richie' , 'CPP' => 'Bjarne', 'Java'
=> 'Sun', 'PHP'=>'Orelly');
$middle = array_slice($book, 1, 2);
print_r($middle);
//Array ( [CPP] => Bjarne [Java] => Sun )
Monica Deshmane(H.V.Desai college,Pune) 10
Splitting of an array into chunks
Which parameters required?
?
$newarr=array_chunk($arr, chunk_size);
$nums = range(1, 7);
$rows = array_chunk($nums, 3);
print_r($rows);
// Array ( [0] => Array ( [0] => 1 [1] => 2 [2] => 3 )
[1] => Array ( [0] => 4 [1] => 5 [2] => 6 ) [2] =>
Array ( [0] => 7 ) )
Monica Deshmane(H.V.Desai college,Pune) 11
Keys
Which parameters required?
?
$array_of_keys = array_keys(array);
$book = array('C' => 'Richie' , 'CPP' => 'Bjarne', 'Java'
=> 'Sun', 'PHP'=>'Orelly');
$keys = array_keys($book);
//Array ( [0] => C [1] => CPP [2] => Java [3] => PHP )
Monica Deshmane(H.V.Desai college,Pune) 12
Values
Which parameters required?
?
$array_of_values = array_values(array);
$values = array_values($book);
print_r($values);
//Array ( [0] => Richie [1] => Bjarne [2] => Sun [3]
=> Orelly )
Monica Deshmane(H.V.Desai college,Pune) 13
To check perticular key
1) Checking Whether an Element Exists
$flag = array_key_exists(key, array)
$book = array('C' => 'Richie' , 'CPP' => 'Bjarne', 'Java'
=> 'Sun', 'PHP'=>'Orelly');
$b = array_key_exists('Java', $book);
echo "Key exist : ".$b;
Returns true if exists false otherwise.
2)isset()
Same as array key exists but the difference is if key
contains value null it not returns true.
Monica Deshmane(H.V.Desai college,Pune) 14
To check perticular value
1) Checking Whether an Element Exists
$flag = in_array(value, array)
$book = array('C' => 'Richie' , 'CPP' => 'Bjarne', 'Java'
=> 'Sun', 'PHP'=>'Orelly');
$b =in_array (‘sun', $book);
echo “value exist : ".$b;
Returns true if exists false otherwise.
Monica Deshmane(H.V.Desai college,Pune) 15
In_array in deep-Searching an array
in_array(search,array,type)
//type for strict type checking-true/false
$book = array("C", "CPP", "Java", "PHP", 10);
if (in_array("CPP",$book))
{
echo "Match found<br />";
}
else
{
echo "Match not found<br />";
}
//Match found
Monica Deshmane(H.V.Desai college,Pune) 16
In_array in deep-Searching an array
$book = array("C", "CPP", "Java", "PHP", 10);
if (in_array(“10",$book))
{
echo "Match found<br />";
}
else
{
echo "Match not found<br />";
}
//Match found
Monica Deshmane(H.V.Desai college,Pune) 17
Searching an array
$book = array("C", "CPP", "Java", "PHP", 10);
if (in_array(“10",$book,true))
{
echo "Match found<br />";
}
else
{
echo "Match not found<br />";
}
//Match not found
Monica Deshmane(H.V.Desai college,Pune) 18
2)Array_search()
Same parameters as in_array()
Kay= array_search(value to serch,array);
$arr=array(“r"=>"red",“g"=>"green",“b"=>"blue");
echo array_search("red",$arr);
//returns key if value found
Output-
r
Monica Deshmane(H.V.Desai college,Pune) 19
Revise…….
Extracting multiple values/ multiple variable assignment
List($a,$b)=$arr;
Slicing array-
$middle = array_slice($book, 1, 2);
Retrieving array of keys -
$array_of_keys = array_keys(array);
Retrieving array of values -
$array_of_keys = array_values(array);
Monica Deshmane(H.V.Desai college,Pune) 20
Revise…….
Splitting of an array into chunks
$newarr=array_chunk($arr, chunk_size);
1) Checking Whether an Element key Exists
1)$flag = array_key_exists(key, array)
2)Isset()
1) Checking Whether an Element value Exists
In_array(value,array,type)
2) echo array_search("red",$arr);
Monica Deshmane(H.V.Desai college,Pune) 21
Splice()
Removing and inserting elements in an array
Which parameters required?
?
$removed = array_splice(array, start,
[, length
[, replacement] ]);
Monica Deshmane(H.V.Desai college,Pune) 22
Splice continue….
1. For removing array elements from start till length:
$sub = array('C', 'CPP', 'Java', 'PPL', 'DS', 'DBMS',
'SDK','ML');
$removed = array_splice($sub, 3, 2);
print_r($removed);
echo "<br>";
print_r($sub);
// Array ( [0] => PPL [1] => DS )
// Array ( [0] => C [1] => CPP [2] => Java [3] =>
DBMS [4] => SDK [5] => ML)
Monica Deshmane(H.V.Desai college,Pune) 23
Splice continue….
2. For removing array elements to end of array:
$sub = array('C', 'CPP', 'Java', 'PHP', 'DS', 'DBMS',
‘NODE JS','ML');
$removed = array_splice($sub, 3);
print_r($removed);
echo "<br>";
print_r($sub);
// Array ( [0] => PHP [1] => DS [2] => DBMS [3] =>
NODE JS [4] => ML )
// Array ( [0] => C [1] => CPP [2] => Java )
Monica Deshmane(H.V.Desai college,Pune) 24
Splice continue….
3. For inserting/ replacing array elements in an array:
$new_arr = array('Syspro', 'TCS', 'DDC');
$sub = array('C', 'CPP', 'Java', 'PHP', 'DS', 'DBMS',
'SDK','MFC');
$removed = array_splice($sub, 3, 2, $new_arr);
print_r($removed);
echo "<br>";
print_r($sub);
// Array ( [0] => PHP [1] => DS )
// Array ( [0] => C [1] => CPP [2] => Java [3] =>
Syspro [4] => TCS [5] => DDC [6] => MFC )
Monica Deshmane(H.V.Desai college,Pune) 25
Splice continue….
4. For inserting array elements in an array without deleting:
$new_arr = array('Syspro', 'TCS', 'DAA');
$sub = array('C', 'CPP', 'Java', 'PHP', 'DS', 'DBMS',
'SDK','MFC');
$removed = array_splice($sub, 3, 0, $new_arr);
print_r($removed);
echo "<br>";
print_r($sub);
//Array ( )
//Array ( [0] => C [1] => CPP [2] => Java [3] => Syspro [4]
=> TCS [5] => DAA [6] => PHP [7] => DS [8] => DBMS [9]
=> SDK [10] => MFC )
Monica Deshmane(H.V.Desai college,Pune) 26
Removing and inserting elements in an array
5. For removing array elements from associative
array:
$book = array('C' => 'Richie' , 'CPP' => 'Bjarne', 'Java'
=> 'Sun', 'PHP'=>'Orelly');
$removed = array_splice($book, 1,2);
print_r($removed);
echo "<br>";
print_r($book);
//Array ( [CPP] => Bjarne [Java] => Sun )
//Array ( [C] => Richie [PHP] => Orelly )
Monica Deshmane(H.V.Desai college,Pune) 27
Splice continue….
6. For inserting array elements into associative array:
$new_arr = array(‘new'=>‘xyz');
$book = array('C' => 'Richie' , 'CPP' => 'Bjarne', 'Java'
=> 'Sun', 'PHP'=>'Orelly');
$removed = array_splice($book, 1,0, $new_arr);
//0 for insert without delete
print_r($removed);
echo "<br>";
print_r($book);
//Array()
//Array ( [C] => Richie [0] => xyz [CPP] => Bjarne
[Java] => Sun [PHP] => Orelly )
Monica Deshmane(H.V.Desai college,Pune) 28

More Related Content

KEY
Extending Moose
PPTX
Chap 3php array part 3
PPTX
Drupal 8 database api
PPTX
Chap 3php array part4
PDF
Database API, your new friend
PPTX
php string part 3
PPTX
Topological indices (t is) of the graphs to seek qsar models of proteins com...
PPTX
第二讲 Python基礎
Extending Moose
Chap 3php array part 3
Drupal 8 database api
Chap 3php array part4
Database API, your new friend
php string part 3
Topological indices (t is) of the graphs to seek qsar models of proteins com...
第二讲 Python基礎

What's hot (20)

PPTX
第二讲 预备-Python基礎
PDF
Magicke metody v Pythonu
PDF
Python Puzzlers
PDF
Object Orientation vs Functional Programming in Python
PDF
Doctrator Symfony Live 2011 San Francisco
PPT
PHP and MySQL
PDF
Perl object ?
PDF
Introdução ao Perl 6
PDF
Symfony2 and Doctrine2 Integration
PDF
Arrays in PHP
PDF
Smelling your code
KEY
Can't Miss Features of PHP 5.3 and 5.4
DOCX
PERL for QA - Important Commands and applications
PPTX
Drupal7 dbtng
PDF
Dependency Injection with PHP and PHP 5.3
PDF
Descobrindo a linguagem Perl
PDF
From mysql to MongoDB(MongoDB2011北京交流会)
PDF
집단지성 프로그래밍 08-가격모델링
PPT
Class 4 - PHP Arrays
第二讲 预备-Python基礎
Magicke metody v Pythonu
Python Puzzlers
Object Orientation vs Functional Programming in Python
Doctrator Symfony Live 2011 San Francisco
PHP and MySQL
Perl object ?
Introdução ao Perl 6
Symfony2 and Doctrine2 Integration
Arrays in PHP
Smelling your code
Can't Miss Features of PHP 5.3 and 5.4
PERL for QA - Important Commands and applications
Drupal7 dbtng
Dependency Injection with PHP and PHP 5.3
Descobrindo a linguagem Perl
From mysql to MongoDB(MongoDB2011北京交流会)
집단지성 프로그래밍 08-가격모델링
Class 4 - PHP Arrays
Ad

Similar to Chap 3php array part 2 (20)

PPTX
Chap 3php array part1
PPT
Using arrays with PHP for forms and storing information
PPTX
5 Arry in PHP.pptxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
PPTX
Chapter 2 wbp.pptx
PPTX
PHP array 1
PPT
Arrays in php
PPT
PHP - Introduction to PHP Arrays
PPTX
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
PDF
Array String - Web Programming
PDF
Php array
PPT
PHP array 2
PPTX
PHP Arrays_Introduction
PPTX
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
PPTX
UNIT IV (4).pptx
PPTX
Unit 2-Arrays.pptx
PPT
Web Technology - PHP Arrays
PPTX
Introduction to php 6
PPTX
Arrays in PHP
PPT
PHP-04-Arrays.ppt
DOCX
PHP record- with all programs and output
Chap 3php array part1
Using arrays with PHP for forms and storing information
5 Arry in PHP.pptxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
Chapter 2 wbp.pptx
PHP array 1
Arrays in php
PHP - Introduction to PHP Arrays
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
Array String - Web Programming
Php array
PHP array 2
PHP Arrays_Introduction
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
UNIT IV (4).pptx
Unit 2-Arrays.pptx
Web Technology - PHP Arrays
Introduction to php 6
Arrays in PHP
PHP-04-Arrays.ppt
PHP record- with all programs and output
Ad

More from monikadeshmane (17)

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

Recently uploaded (20)

PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
RMMM.pdf make it easy to upload and study
PDF
Classroom Observation Tools for Teachers
PPTX
Institutional Correction lecture only . . .
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PPTX
Pharma ospi slides which help in ospi learning
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Microbial diseases, their pathogenesis and prophylaxis
Microbial disease of the cardiovascular and lymphatic systems
Final Presentation General Medicine 03-08-2024.pptx
202450812 BayCHI UCSC-SV 20250812 v17.pptx
VCE English Exam - Section C Student Revision Booklet
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
RMMM.pdf make it easy to upload and study
Classroom Observation Tools for Teachers
Institutional Correction lecture only . . .
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
O7-L3 Supply Chain Operations - ICLT Program
Final Presentation General Medicine 03-08-2024.pptx
2.FourierTransform-ShortQuestionswithAnswers.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
O5-L3 Freight Transport Ops (International) V1.pdf
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
Pharma ospi slides which help in ospi learning
FourierSeries-QuestionsWithAnswers(Part-A).pdf

Chap 3php array part 2

  • 1. Chapter 3 PHP Array-part 2 1 Monica Deshmane(H.V.Desai college,Pune)
  • 2. Topics • Extracting multiple values • Slicing an array • Splitting of an array into chunks • Retrieving keys & values • Searching key or values • splice Monica Deshmane(H.V.Desai college,Pune) 2
  • 4. Introduction •An array is a special variable, which can hold more than one value at a time. •It means it is continuous block of memory locations. •Arrays can be categorised using dimensions. •1-D array •2-D Array •Multidimenstional array. •In each category we have 2 types of arrays- •Indexed Array •Associative Array Monica Deshmane(H.V.Desai college,Pune) 4
  • 5. Indexed Vs Associative Array Indexed Arrays: The keys of an indexed array are integers, beginning at 0. Indexed arrays are used when you identify things by their position. Associative arrays: This array contains strings as keys and behave more like two-column tables. The first column is the key, which is used to access the value. PHP internally stores all arrays as associative arrays. Monica Deshmane(H.V.Desai college,Pune) 5
  • 6. Extracting multiple values OR multiple variable assignment To copy all of an array's values into variables, use the list( ) construct: Which parameters required? ? array list($variable, ...) = $array; Monica Deshmane(H.V.Desai college,Pune) 6
  • 7. Extracting multiple values/ multiple variable assignment $book = array('C', 'Dennis Richie', 500); list($n, $a, $p) = $book; echo "Values : ".$n." ".$a." ".$p; //Values : C Dennis Richie 500 $book = array('C', 'Dennis Richie', 500); list($n, $a) = $book; echo "<br> Values : ".$n." ".$a." "; //Values : C Dennis Richie Monica Deshmane(H.V.Desai college,Pune) 7
  • 8. Extracting multiple values $inventor= array('C', 'Dennis Richie'); list($n, $a, $p) = $inventor; echo "<br> Values : ".$n." ".$a." ".$p; //Values : C Dennis Richie NULL $values = range('a', 'e'); list($m,,$n,,$o) = $values; //,, for alternate elements echo "<br> Values : ".$m." ".$n." ".$o; //Values : a c e Monica Deshmane(H.V.Desai college,Pune) 8
  • 9. Slicing an array To retrieve sub part of array Which parameters required? ? Array Start index No. of locations $subset = array_slice(array, offset,[ length]); Monica Deshmane(H.V.Desai college,Pune) 9
  • 10. Slicing an array $sub = array('C', 'CPP', 'Java', 'PHP', 'DS'); $middle = array_slice($sub, 2, 3); print_r($middle); //Array ( [0] => Java [1] => PHP [2] => DS ) $book = array('C' => 'Richie' , 'CPP' => 'Bjarne', 'Java' => 'Sun', 'PHP'=>'Orelly'); $middle = array_slice($book, 1, 2); print_r($middle); //Array ( [CPP] => Bjarne [Java] => Sun ) Monica Deshmane(H.V.Desai college,Pune) 10
  • 11. Splitting of an array into chunks Which parameters required? ? $newarr=array_chunk($arr, chunk_size); $nums = range(1, 7); $rows = array_chunk($nums, 3); print_r($rows); // Array ( [0] => Array ( [0] => 1 [1] => 2 [2] => 3 ) [1] => Array ( [0] => 4 [1] => 5 [2] => 6 ) [2] => Array ( [0] => 7 ) ) Monica Deshmane(H.V.Desai college,Pune) 11
  • 12. Keys Which parameters required? ? $array_of_keys = array_keys(array); $book = array('C' => 'Richie' , 'CPP' => 'Bjarne', 'Java' => 'Sun', 'PHP'=>'Orelly'); $keys = array_keys($book); //Array ( [0] => C [1] => CPP [2] => Java [3] => PHP ) Monica Deshmane(H.V.Desai college,Pune) 12
  • 13. Values Which parameters required? ? $array_of_values = array_values(array); $values = array_values($book); print_r($values); //Array ( [0] => Richie [1] => Bjarne [2] => Sun [3] => Orelly ) Monica Deshmane(H.V.Desai college,Pune) 13
  • 14. To check perticular key 1) Checking Whether an Element Exists $flag = array_key_exists(key, array) $book = array('C' => 'Richie' , 'CPP' => 'Bjarne', 'Java' => 'Sun', 'PHP'=>'Orelly'); $b = array_key_exists('Java', $book); echo "Key exist : ".$b; Returns true if exists false otherwise. 2)isset() Same as array key exists but the difference is if key contains value null it not returns true. Monica Deshmane(H.V.Desai college,Pune) 14
  • 15. To check perticular value 1) Checking Whether an Element Exists $flag = in_array(value, array) $book = array('C' => 'Richie' , 'CPP' => 'Bjarne', 'Java' => 'Sun', 'PHP'=>'Orelly'); $b =in_array (‘sun', $book); echo “value exist : ".$b; Returns true if exists false otherwise. Monica Deshmane(H.V.Desai college,Pune) 15
  • 16. In_array in deep-Searching an array in_array(search,array,type) //type for strict type checking-true/false $book = array("C", "CPP", "Java", "PHP", 10); if (in_array("CPP",$book)) { echo "Match found<br />"; } else { echo "Match not found<br />"; } //Match found Monica Deshmane(H.V.Desai college,Pune) 16
  • 17. In_array in deep-Searching an array $book = array("C", "CPP", "Java", "PHP", 10); if (in_array(“10",$book)) { echo "Match found<br />"; } else { echo "Match not found<br />"; } //Match found Monica Deshmane(H.V.Desai college,Pune) 17
  • 18. Searching an array $book = array("C", "CPP", "Java", "PHP", 10); if (in_array(“10",$book,true)) { echo "Match found<br />"; } else { echo "Match not found<br />"; } //Match not found Monica Deshmane(H.V.Desai college,Pune) 18
  • 19. 2)Array_search() Same parameters as in_array() Kay= array_search(value to serch,array); $arr=array(“r"=>"red",“g"=>"green",“b"=>"blue"); echo array_search("red",$arr); //returns key if value found Output- r Monica Deshmane(H.V.Desai college,Pune) 19
  • 20. Revise……. Extracting multiple values/ multiple variable assignment List($a,$b)=$arr; Slicing array- $middle = array_slice($book, 1, 2); Retrieving array of keys - $array_of_keys = array_keys(array); Retrieving array of values - $array_of_keys = array_values(array); Monica Deshmane(H.V.Desai college,Pune) 20
  • 21. Revise……. Splitting of an array into chunks $newarr=array_chunk($arr, chunk_size); 1) Checking Whether an Element key Exists 1)$flag = array_key_exists(key, array) 2)Isset() 1) Checking Whether an Element value Exists In_array(value,array,type) 2) echo array_search("red",$arr); Monica Deshmane(H.V.Desai college,Pune) 21
  • 22. Splice() Removing and inserting elements in an array Which parameters required? ? $removed = array_splice(array, start, [, length [, replacement] ]); Monica Deshmane(H.V.Desai college,Pune) 22
  • 23. Splice continue…. 1. For removing array elements from start till length: $sub = array('C', 'CPP', 'Java', 'PPL', 'DS', 'DBMS', 'SDK','ML'); $removed = array_splice($sub, 3, 2); print_r($removed); echo "<br>"; print_r($sub); // Array ( [0] => PPL [1] => DS ) // Array ( [0] => C [1] => CPP [2] => Java [3] => DBMS [4] => SDK [5] => ML) Monica Deshmane(H.V.Desai college,Pune) 23
  • 24. Splice continue…. 2. For removing array elements to end of array: $sub = array('C', 'CPP', 'Java', 'PHP', 'DS', 'DBMS', ‘NODE JS','ML'); $removed = array_splice($sub, 3); print_r($removed); echo "<br>"; print_r($sub); // Array ( [0] => PHP [1] => DS [2] => DBMS [3] => NODE JS [4] => ML ) // Array ( [0] => C [1] => CPP [2] => Java ) Monica Deshmane(H.V.Desai college,Pune) 24
  • 25. Splice continue…. 3. For inserting/ replacing array elements in an array: $new_arr = array('Syspro', 'TCS', 'DDC'); $sub = array('C', 'CPP', 'Java', 'PHP', 'DS', 'DBMS', 'SDK','MFC'); $removed = array_splice($sub, 3, 2, $new_arr); print_r($removed); echo "<br>"; print_r($sub); // Array ( [0] => PHP [1] => DS ) // Array ( [0] => C [1] => CPP [2] => Java [3] => Syspro [4] => TCS [5] => DDC [6] => MFC ) Monica Deshmane(H.V.Desai college,Pune) 25
  • 26. Splice continue…. 4. For inserting array elements in an array without deleting: $new_arr = array('Syspro', 'TCS', 'DAA'); $sub = array('C', 'CPP', 'Java', 'PHP', 'DS', 'DBMS', 'SDK','MFC'); $removed = array_splice($sub, 3, 0, $new_arr); print_r($removed); echo "<br>"; print_r($sub); //Array ( ) //Array ( [0] => C [1] => CPP [2] => Java [3] => Syspro [4] => TCS [5] => DAA [6] => PHP [7] => DS [8] => DBMS [9] => SDK [10] => MFC ) Monica Deshmane(H.V.Desai college,Pune) 26
  • 27. Removing and inserting elements in an array 5. For removing array elements from associative array: $book = array('C' => 'Richie' , 'CPP' => 'Bjarne', 'Java' => 'Sun', 'PHP'=>'Orelly'); $removed = array_splice($book, 1,2); print_r($removed); echo "<br>"; print_r($book); //Array ( [CPP] => Bjarne [Java] => Sun ) //Array ( [C] => Richie [PHP] => Orelly ) Monica Deshmane(H.V.Desai college,Pune) 27
  • 28. Splice continue…. 6. For inserting array elements into associative array: $new_arr = array(‘new'=>‘xyz'); $book = array('C' => 'Richie' , 'CPP' => 'Bjarne', 'Java' => 'Sun', 'PHP'=>'Orelly'); $removed = array_splice($book, 1,0, $new_arr); //0 for insert without delete print_r($removed); echo "<br>"; print_r($book); //Array() //Array ( [C] => Richie [0] => xyz [CPP] => Bjarne [Java] => Sun [PHP] => Orelly ) Monica Deshmane(H.V.Desai college,Pune) 28