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
PPTX
PHP string-part 1
PPTX
Chapter 2 wbp.pptx
PPTX
java script
PPTX
Arrays syntax and it's functions in php.pptx
PPTX
Array in php
PDF
Php tips-and-tricks4128
PPTX
Php Basics Iterations, looping
PDF
Adventures in Optimization
PPTX
php string part 4
PPTX
PHP Functions & Arrays
PPTX
Array functions for all languages prog.pptx
PPTX
Array functions using php programming language.pptx
PPTX
Php functions
PDF
Practice exam php
PPTX
Chap1introppt2php(finally done)
PPTX
Php & my sql
PPTX
Programming Basics - array, loops, funcitons
PDF
PHP Conference Asia 2016
PDF
WordCamp Portland 2018: PHP for WordPress
Chap 3php array part1
PHP string-part 1
Chapter 2 wbp.pptx
java script
Arrays syntax and it's functions in php.pptx
Array in php
Php tips-and-tricks4128
Php Basics Iterations, looping
Adventures in Optimization
php string part 4
PHP Functions & Arrays
Array functions for all languages prog.pptx
Array functions using php programming language.pptx
Php functions
Practice exam php
Chap1introppt2php(finally done)
Php & my sql
Programming Basics - array, loops, funcitons
PHP Conference Asia 2016
WordCamp Portland 2018: PHP for WordPress
Ad

More from monikadeshmane (13)

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 2
PPT
ip1clientserver model
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 2
ip1clientserver model
Chap1introppt1php basic

Recently uploaded (20)

PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PPTX
NOI Hackathon - Summer Edition - GreenThumber.pptx
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
Onica Farming 24rsclub profitable farm business
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Open Quiz Monsoon Mind Game Prelims.pptx
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PDF
Pre independence Education in Inndia.pdf
PDF
From loneliness to social connection charting
PDF
Open folder Downloads.pdf yes yes ges yes
PPTX
UNDER FIVE CLINICS OR WELL BABY CLINICS.pptx
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PDF
Piense y hagase Rico - Napoleon Hill Ccesa007.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
NOI Hackathon - Summer Edition - GreenThumber.pptx
human mycosis Human fungal infections are called human mycosis..pptx
Onica Farming 24rsclub profitable farm business
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Open Quiz Monsoon Mind Game Prelims.pptx
Abdominal Access Techniques with Prof. Dr. R K Mishra
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
Week 4 Term 3 Study Techniques revisited.pptx
Pre independence Education in Inndia.pdf
From loneliness to social connection charting
Open folder Downloads.pdf yes yes ges yes
UNDER FIVE CLINICS OR WELL BABY CLINICS.pptx
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
Piense y hagase Rico - Napoleon Hill Ccesa007.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx

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