SlideShare uma empresa Scribd logo
1 de 24
Chapter 3
PHP
Array part-4
1
Monica Deshmane(H.V.Desai college,Pune)
Topics
Sorting Arrays
Monica Deshmane(H.V.Desai college,Pune) 2
Sorting a array
Effect Ascending Descending User-defined
order
Sort array by
values, then
reassign indexes
starting with 0
sort() rsort() usort()
Sort array by
Values
asort() arsort() uarsort()
Sort array by
keys
ksort() krsort() ukrsort()
Monica Deshmane(H.V.Desai college,Pune) 3
Sorting a indexed array
1) sort()
•The sort() function sorts an array by the values.
•This function assigns new keys for the elements in
the array. Existing keys will be removed.
•This function returns TRUE on success, or FALSE
on failure.
$arr = array(10,30,20,15);
sort($arr );
print_r($arr );
//Array ( [0] => 10 [1] => 15 [2] => 20 [3] => 30 )
Monica Deshmane(H.V.Desai college,Pune) 4
Sorting a indexed array
If associative array-
$book = array('Java' => 'Sun', 'CPP' => 'Bjarne', 'C'
=> 'Richie', 'PHP'=>'Orelly');
sort($book );
print_r($book );
//Array ( [0] => Bjarne [1] => Orelly [2] => Richie
[3] => Sun )
Monica Deshmane(H.V.Desai college,Pune) 5
Sorting a indexed array
2) rsort()
•The rsort() function sorts an array by the values in reverse
order.
•This function assigns new keys for the elements in the
array. Existing keys will be removed.
•This function returns TRUE on success, or FALSE on
failure.
$arr = array(10,30,20,15);
rsort($arr );
print_r($arr );
//Array ( [0] => 30 [1] => 20 [2] => 15 [3] => 10 )
Monica Deshmane(H.V.Desai college,Pune) 6
Sorting a indexed array
$book = array('Java' => 'Sun', 'CPP' => 'Bjarne', 'C' =>
'Richie', 'PHP'=>'Orelly');
rsort($book );
print_r($book );
//Array ( [0] => Sun [1] => Richie [2] => Orelly [3] =>
Bjarne )
Monica Deshmane(H.V.Desai college,Pune) 7
Sorting a indexed array
3) usort()
•The usort() function sorts an array by a user defined
comparison function.
•This function assigns new keys for the elements in
the array. Existing keys will be removed.
•This function returns TRUE on success, or FALSE
on failure.
Monica Deshmane(H.V.Desai college,Pune) 8
usort() continue…
function my_sort($a, $b)
{
if ($a == $b) return 0;
return ($a < $b) ? -1 : 1;
}
$arr = array(10,30,25,15);
usort($arr , "my_sort");
print_r ($arr);
//Array ( [0] => 10 [1] => 15 [2] => 25 [3] => 30 )
Monica Deshmane(H.V.Desai college,Pune) 9
Monica Deshmane(H.V.Desai college,Pune) 10
Sorting an associated array
-with values
Sorting an associated array-with values
1)asort()
•The asort() function sorts an array by the values. The
values keep their original keys.
•This function returns TRUE on success, or FALSE
on failure.
$book = array('Java' => 'Sun', 'CPP' => 'Bjarne', 'C'
=> 'Richie', 'PHP'=>'Orelly');
asort($book );
print_r ($book);
//Array ( [CPP] => Bjarne [PHP] => Orelly [C] =>
Richie [Java] => Sun )
Monica Deshmane(H.V.Desai college,Pune) 11
Sorting an associated array-with values
2)arsort()
•The asort() function sorts an array by the values in
reverse order. The values keep their original keys.
•This function returns TRUE on success, or FALSE
on failure.
$book = array('Java' => 'Sun', 'CPP' => 'Bjarne', 'C'
=> 'Richie', 'PHP'=>'Orelly');
arsort($book );
print_r ($book);
//Array ([Java] => Sun [C] => Richie [PHP] =>
Orelly [CPP] => Bjarne)
Monica Deshmane(H.V.Desai college,Pune) 12
ausort()
function my_sort($a, $b)
{
if ($a == $b) return 0;
return ($a < $b) ? -1 : 1;
}
$book = array('Java' => 'Sun', 'CPP' => 'Bjarne', 'C'
=> 'Richie', 'PHP'=>'Orelly');
ausort($book , "my_sort");
print_r ($book);
//Array ( [0] => Bjarne [1] => Orelly [2] => Richie
[3] => Sun )
Monica Deshmane(H.V.Desai college,Pune) 13
Monica Deshmane(H.V.Desai college,Pune) 14
Sorting an associated array
-with keys
Sorting an associated array-with keys
1)ksort()
•The ksort() function sorts an array by the keys. The
values keep their original keys.
•This function returns TRUE on success, or FALSE
on failure.
$book = array('Java' => 'Sun', 'CPP' => 'Bjarne', 'C'
=> 'Richie', 'PHP'=>'Orelly');
ksort($book );
print_r ($book);
//Array ( [C] => Richie [CPP] => Bjarne [Java] =>
Sun [PHP] => Orelly )
Monica Deshmane(H.V.Desai college,Pune) 15
Sorting an associated array-with keys
2)krsort()
•The ksort() function sorts an array by the keys in
reverse order. The values keep their original keys.
•This function returns TRUE on success, or FALSE
on failure.
$book = array('Java' => 'Sun', 'CPP' => 'Bjarne', 'C'
=> 'Richie', 'PHP'=>'Orelly');
krsort($book );
print_r ($book);
//Array ([PHP] => Orelly [Java] => Sun [CPP] =>
Bjarne [C] => Richie)
Monica Deshmane(H.V.Desai college,Pune) 16
Try…..
3)uksort()
Monica Deshmane(H.V.Desai college,Pune) 17
Natural order sorting
$output = natsort(input);
sort() functions correctly sort strings and numbers, but
they don't correctly sort strings that contain numbers.
$temp_files = array("temp15.txt","temp10.txt",
"temp1.txt","temp22.txt","temp2.txt");
sort($temp_files);
echo "Standard sorting: ";
print_r($temp_files);
echo "<br />";
natsort($temp_files);
echo "Natural order: ";
print_r($temp_files);
Monica Deshmane(H.V.Desai college,Pune) 18
Natural order sorting
Output:
Standard sorting: Array ( [0] => temp1.txt [1] =>
temp10.txt [2] => temp15.txt [3] => temp2.txt [4] =>
temp22.txt )
Natural order: Array ( [0] => temp1.txt [3] =>
temp2.txt [1] => temp10.txt [2] => temp15.txt [4] =>
temp22.txt )
Monica Deshmane(H.V.Desai college,Pune) 19
Natural order sorting with case insensitive manner
$output = natcasesort(input);
This function sorts an array by using a "natural order"
algorithm. The values keep their original keys.
This is case-insensitive.
$temp_files = array("temp15.txt","Temp10.txt",
"temp1.txt","Temp22.txt","temp2.txt");
natsort($temp_files);
echo "Natural order: ";
print_r($temp_files);
echo "<br />";
natcasesort($temp_files);
echo "Natural order case insensitve: ";
print_r($temp_files);
Monica Deshmane(H.V.Desai college,Pune) 20
Natural order sorting
Output:
Natural order: Array ( [1] => Temp10.txt [3] =>
Temp22.txt [2] => temp1.txt [4] => temp2.txt [0] =>
temp15.txt )
Natural order case insensitve: Array ( [2] =>
temp1.txt [4] => temp2.txt [1] => Temp10.txt [0] =>
temp15.txt [3] => Temp22.txt )
Monica Deshmane(H.V.Desai college,Pune) 21
Sorting multiple array at once
array_multisort(array1,sorting order,[sorting
type,array2,array3...])
•array1 (required) Specifies an array
•sorting order (optional) Specifies the sorting order.
SORT_ASC Sort in ascending order (A-Z)
SORT_DESC Sort in descending order (Z-A)
•sorting type (optional) Specifies the type to use, when
comparing elements.
SORT_REGULAR Compare elements normally
SORT_NUMERIC Compare elements as numeric
values
SORT_STRING Compare elements as string
values
array2 (Optional) Specifies an array
array3 (Optional) Specifies an array
Monica Deshmane(H.V.Desai college,Pune) 22
Sorting multiple array at once
All array size should be the same.
$sub = array("C", "Java", "CPP", "PHP");
$author = array("Richie", "Sun", "Bjarne", "Orelly");
$price = array(300, 500, 400, 700);
$b = array_multisort($sub, SORT_ASC, $author, $price);
print_r($sub);
print_r($author);
print_r($price);
Output:
Array ( [0] => C [1] => CPP [2] => Java [3] => PHP )
Array ( [0] =>Bjarne [1]=>Orelly [2]=>Richie [3]=>Sun )
Array ( [0] => 300 [1] => 400 [2] => 500 [3] => 700 )
Monica Deshmane(H.V.Desai college,Pune) 23
Revise….
Indexed- Sort()
Rsort()
Usort()
Associative-
Values- Asort()
Arsort()
Uasort()
Kays- Ksort()
Krsort()
Uksort()
Alphanumeric values-
Natsort()
Natcasesort()
Multiple array sort-
Array_multisort()
Monica Deshmane(H.V.Desai college,Pune) 24

Mais conteúdo relacionado

Mais procurados

How to Become a Tree Hugger: Random Forests and Predictive Modeling for Devel...
How to Become a Tree Hugger: Random Forests and Predictive Modeling for Devel...How to Become a Tree Hugger: Random Forests and Predictive Modeling for Devel...
How to Become a Tree Hugger: Random Forests and Predictive Modeling for Devel...Matt Harrison
 
Probabilistic Programming in Scala
Probabilistic Programming in ScalaProbabilistic Programming in Scala
Probabilistic Programming in ScalaBeScala
 
Python Usage (5-minute-summary)
Python Usage (5-minute-summary)Python Usage (5-minute-summary)
Python Usage (5-minute-summary)Ohgyun Ahn
 
Python Puzzlers - 2016 Edition
Python Puzzlers - 2016 EditionPython Puzzlers - 2016 Edition
Python Puzzlers - 2016 EditionNandan Sawant
 
Cheat sheet python3
Cheat sheet python3Cheat sheet python3
Cheat sheet python3sxw2k
 
iOS와 케라스의 만남
iOS와 케라스의 만남iOS와 케라스의 만남
iOS와 케라스의 만남Mijeong Jeon
 
Phylogenetics in R
Phylogenetics in RPhylogenetics in R
Phylogenetics in Rschamber
 
프알못의 Keras 사용기
프알못의 Keras 사용기프알못의 Keras 사용기
프알못의 Keras 사용기Mijeong Jeon
 
Pa1 session 3_slides
Pa1 session 3_slidesPa1 session 3_slides
Pa1 session 3_slidesaiclub_slides
 

Mais procurados (20)

How to Become a Tree Hugger: Random Forests and Predictive Modeling for Devel...
How to Become a Tree Hugger: Random Forests and Predictive Modeling for Devel...How to Become a Tree Hugger: Random Forests and Predictive Modeling for Devel...
How to Become a Tree Hugger: Random Forests and Predictive Modeling for Devel...
 
Arrays in PHP
Arrays in PHPArrays in PHP
Arrays in PHP
 
Python programming : List and tuples
Python programming : List and tuplesPython programming : List and tuples
Python programming : List and tuples
 
Python-Tuples
Python-TuplesPython-Tuples
Python-Tuples
 
Probabilistic Programming in Scala
Probabilistic Programming in ScalaProbabilistic Programming in Scala
Probabilistic Programming in Scala
 
Python Usage (5-minute-summary)
Python Usage (5-minute-summary)Python Usage (5-minute-summary)
Python Usage (5-minute-summary)
 
Next Level Testing
Next Level TestingNext Level Testing
Next Level Testing
 
R programming
R programmingR programming
R programming
 
Python_ 3 CheatSheet
Python_ 3 CheatSheetPython_ 3 CheatSheet
Python_ 3 CheatSheet
 
Tuple in python
Tuple in pythonTuple in python
Tuple in python
 
Python Puzzlers - 2016 Edition
Python Puzzlers - 2016 EditionPython Puzzlers - 2016 Edition
Python Puzzlers - 2016 Edition
 
Cheat sheet python3
Cheat sheet python3Cheat sheet python3
Cheat sheet python3
 
iOS와 케라스의 만남
iOS와 케라스의 만남iOS와 케라스의 만남
iOS와 케라스의 만남
 
Fp java8
Fp java8Fp java8
Fp java8
 
Scala Parallel Collections
Scala Parallel CollectionsScala Parallel Collections
Scala Parallel Collections
 
List in Python
List in PythonList in Python
List in Python
 
Scala collections
Scala collectionsScala collections
Scala collections
 
Phylogenetics in R
Phylogenetics in RPhylogenetics in R
Phylogenetics in R
 
프알못의 Keras 사용기
프알못의 Keras 사용기프알못의 Keras 사용기
프알못의 Keras 사용기
 
Pa1 session 3_slides
Pa1 session 3_slidesPa1 session 3_slides
Pa1 session 3_slides
 

Semelhante a Chap 3php array part4

Chap 3php array part 2
Chap 3php array part 2Chap 3php array part 2
Chap 3php array part 2monikadeshmane
 
Php Chapter 2 3 Training
Php Chapter 2 3 TrainingPhp Chapter 2 3 Training
Php Chapter 2 3 TrainingChris Chubb
 
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...Dhivyaa C.R
 
9780538745840 ppt ch06
9780538745840 ppt ch069780538745840 ppt ch06
9780538745840 ppt ch06Terry Yoast
 
18. Java associative arrays
18. Java associative arrays18. Java associative arrays
18. Java associative arraysIntro C# Book
 
Class 4 - PHP Arrays
Class 4 - PHP ArraysClass 4 - PHP Arrays
Class 4 - PHP ArraysAhmed Swilam
 
Underscore.js
Underscore.jsUnderscore.js
Underscore.jstimourian
 
PHP Functions & Arrays
PHP Functions & ArraysPHP Functions & Arrays
PHP Functions & ArraysHenry Osborne
 
PHP and MySQL Tips and tricks, DC 2007
PHP and MySQL Tips and tricks, DC 2007PHP and MySQL Tips and tricks, DC 2007
PHP and MySQL Tips and tricks, DC 2007Damien Seguy
 
An introduction to property-based testing
An introduction to property-based testingAn introduction to property-based testing
An introduction to property-based testingVincent Pradeilles
 
Google Guava for cleaner code
Google Guava for cleaner codeGoogle Guava for cleaner code
Google Guava for cleaner codeMite Mitreski
 

Semelhante a Chap 3php array part4 (20)

Chap 3php array part 2
Chap 3php array part 2Chap 3php array part 2
Chap 3php array part 2
 
Chap 3php array part1
Chap 3php array part1Chap 3php array part1
Chap 3php array part1
 
Php Chapter 2 3 Training
Php Chapter 2 3 TrainingPhp Chapter 2 3 Training
Php Chapter 2 3 Training
 
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
 
UNIT IV (4).pptx
UNIT IV (4).pptxUNIT IV (4).pptx
UNIT IV (4).pptx
 
9780538745840 ppt ch06
9780538745840 ppt ch069780538745840 ppt ch06
9780538745840 ppt ch06
 
arrays.pdf
arrays.pdfarrays.pdf
arrays.pdf
 
18. Java associative arrays
18. Java associative arrays18. Java associative arrays
18. Java associative arrays
 
Php array
Php arrayPhp array
Php array
 
Class 4 - PHP Arrays
Class 4 - PHP ArraysClass 4 - PHP Arrays
Class 4 - PHP Arrays
 
Chapter 2 wbp.pptx
Chapter 2 wbp.pptxChapter 2 wbp.pptx
Chapter 2 wbp.pptx
 
Underscore.js
Underscore.jsUnderscore.js
Underscore.js
 
PHP Functions & Arrays
PHP Functions & ArraysPHP Functions & Arrays
PHP Functions & Arrays
 
Meet scala
Meet scalaMeet scala
Meet scala
 
PHP and MySQL Tips and tricks, DC 2007
PHP and MySQL Tips and tricks, DC 2007PHP and MySQL Tips and tricks, DC 2007
PHP and MySQL Tips and tricks, DC 2007
 
An introduction to property-based testing
An introduction to property-based testingAn introduction to property-based testing
An introduction to property-based testing
 
php string part 3
php string part 3php string part 3
php string part 3
 
Google Guava for cleaner code
Google Guava for cleaner codeGoogle Guava for cleaner code
Google Guava for cleaner code
 
arrays.ppt
arrays.pptarrays.ppt
arrays.ppt
 
arrays.ppt
arrays.pptarrays.ppt
arrays.ppt
 

Mais de monikadeshmane

Mais de monikadeshmane (17)

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

Último

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseAnaAcapella
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxdhanalakshmis0310
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 

Último (20)

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 

Chap 3php array part4

  • 1. Chapter 3 PHP Array part-4 1 Monica Deshmane(H.V.Desai college,Pune)
  • 3. Sorting a array Effect Ascending Descending User-defined order Sort array by values, then reassign indexes starting with 0 sort() rsort() usort() Sort array by Values asort() arsort() uarsort() Sort array by keys ksort() krsort() ukrsort() Monica Deshmane(H.V.Desai college,Pune) 3
  • 4. Sorting a indexed array 1) sort() •The sort() function sorts an array by the values. •This function assigns new keys for the elements in the array. Existing keys will be removed. •This function returns TRUE on success, or FALSE on failure. $arr = array(10,30,20,15); sort($arr ); print_r($arr ); //Array ( [0] => 10 [1] => 15 [2] => 20 [3] => 30 ) Monica Deshmane(H.V.Desai college,Pune) 4
  • 5. Sorting a indexed array If associative array- $book = array('Java' => 'Sun', 'CPP' => 'Bjarne', 'C' => 'Richie', 'PHP'=>'Orelly'); sort($book ); print_r($book ); //Array ( [0] => Bjarne [1] => Orelly [2] => Richie [3] => Sun ) Monica Deshmane(H.V.Desai college,Pune) 5
  • 6. Sorting a indexed array 2) rsort() •The rsort() function sorts an array by the values in reverse order. •This function assigns new keys for the elements in the array. Existing keys will be removed. •This function returns TRUE on success, or FALSE on failure. $arr = array(10,30,20,15); rsort($arr ); print_r($arr ); //Array ( [0] => 30 [1] => 20 [2] => 15 [3] => 10 ) Monica Deshmane(H.V.Desai college,Pune) 6
  • 7. Sorting a indexed array $book = array('Java' => 'Sun', 'CPP' => 'Bjarne', 'C' => 'Richie', 'PHP'=>'Orelly'); rsort($book ); print_r($book ); //Array ( [0] => Sun [1] => Richie [2] => Orelly [3] => Bjarne ) Monica Deshmane(H.V.Desai college,Pune) 7
  • 8. Sorting a indexed array 3) usort() •The usort() function sorts an array by a user defined comparison function. •This function assigns new keys for the elements in the array. Existing keys will be removed. •This function returns TRUE on success, or FALSE on failure. Monica Deshmane(H.V.Desai college,Pune) 8
  • 9. usort() continue… function my_sort($a, $b) { if ($a == $b) return 0; return ($a < $b) ? -1 : 1; } $arr = array(10,30,25,15); usort($arr , "my_sort"); print_r ($arr); //Array ( [0] => 10 [1] => 15 [2] => 25 [3] => 30 ) Monica Deshmane(H.V.Desai college,Pune) 9
  • 10. Monica Deshmane(H.V.Desai college,Pune) 10 Sorting an associated array -with values
  • 11. Sorting an associated array-with values 1)asort() •The asort() function sorts an array by the values. The values keep their original keys. •This function returns TRUE on success, or FALSE on failure. $book = array('Java' => 'Sun', 'CPP' => 'Bjarne', 'C' => 'Richie', 'PHP'=>'Orelly'); asort($book ); print_r ($book); //Array ( [CPP] => Bjarne [PHP] => Orelly [C] => Richie [Java] => Sun ) Monica Deshmane(H.V.Desai college,Pune) 11
  • 12. Sorting an associated array-with values 2)arsort() •The asort() function sorts an array by the values in reverse order. The values keep their original keys. •This function returns TRUE on success, or FALSE on failure. $book = array('Java' => 'Sun', 'CPP' => 'Bjarne', 'C' => 'Richie', 'PHP'=>'Orelly'); arsort($book ); print_r ($book); //Array ([Java] => Sun [C] => Richie [PHP] => Orelly [CPP] => Bjarne) Monica Deshmane(H.V.Desai college,Pune) 12
  • 13. ausort() function my_sort($a, $b) { if ($a == $b) return 0; return ($a < $b) ? -1 : 1; } $book = array('Java' => 'Sun', 'CPP' => 'Bjarne', 'C' => 'Richie', 'PHP'=>'Orelly'); ausort($book , "my_sort"); print_r ($book); //Array ( [0] => Bjarne [1] => Orelly [2] => Richie [3] => Sun ) Monica Deshmane(H.V.Desai college,Pune) 13
  • 14. Monica Deshmane(H.V.Desai college,Pune) 14 Sorting an associated array -with keys
  • 15. Sorting an associated array-with keys 1)ksort() •The ksort() function sorts an array by the keys. The values keep their original keys. •This function returns TRUE on success, or FALSE on failure. $book = array('Java' => 'Sun', 'CPP' => 'Bjarne', 'C' => 'Richie', 'PHP'=>'Orelly'); ksort($book ); print_r ($book); //Array ( [C] => Richie [CPP] => Bjarne [Java] => Sun [PHP] => Orelly ) Monica Deshmane(H.V.Desai college,Pune) 15
  • 16. Sorting an associated array-with keys 2)krsort() •The ksort() function sorts an array by the keys in reverse order. The values keep their original keys. •This function returns TRUE on success, or FALSE on failure. $book = array('Java' => 'Sun', 'CPP' => 'Bjarne', 'C' => 'Richie', 'PHP'=>'Orelly'); krsort($book ); print_r ($book); //Array ([PHP] => Orelly [Java] => Sun [CPP] => Bjarne [C] => Richie) Monica Deshmane(H.V.Desai college,Pune) 16
  • 18. Natural order sorting $output = natsort(input); sort() functions correctly sort strings and numbers, but they don't correctly sort strings that contain numbers. $temp_files = array("temp15.txt","temp10.txt", "temp1.txt","temp22.txt","temp2.txt"); sort($temp_files); echo "Standard sorting: "; print_r($temp_files); echo "<br />"; natsort($temp_files); echo "Natural order: "; print_r($temp_files); Monica Deshmane(H.V.Desai college,Pune) 18
  • 19. Natural order sorting Output: Standard sorting: Array ( [0] => temp1.txt [1] => temp10.txt [2] => temp15.txt [3] => temp2.txt [4] => temp22.txt ) Natural order: Array ( [0] => temp1.txt [3] => temp2.txt [1] => temp10.txt [2] => temp15.txt [4] => temp22.txt ) Monica Deshmane(H.V.Desai college,Pune) 19
  • 20. Natural order sorting with case insensitive manner $output = natcasesort(input); This function sorts an array by using a "natural order" algorithm. The values keep their original keys. This is case-insensitive. $temp_files = array("temp15.txt","Temp10.txt", "temp1.txt","Temp22.txt","temp2.txt"); natsort($temp_files); echo "Natural order: "; print_r($temp_files); echo "<br />"; natcasesort($temp_files); echo "Natural order case insensitve: "; print_r($temp_files); Monica Deshmane(H.V.Desai college,Pune) 20
  • 21. Natural order sorting Output: Natural order: Array ( [1] => Temp10.txt [3] => Temp22.txt [2] => temp1.txt [4] => temp2.txt [0] => temp15.txt ) Natural order case insensitve: Array ( [2] => temp1.txt [4] => temp2.txt [1] => Temp10.txt [0] => temp15.txt [3] => Temp22.txt ) Monica Deshmane(H.V.Desai college,Pune) 21
  • 22. Sorting multiple array at once array_multisort(array1,sorting order,[sorting type,array2,array3...]) •array1 (required) Specifies an array •sorting order (optional) Specifies the sorting order. SORT_ASC Sort in ascending order (A-Z) SORT_DESC Sort in descending order (Z-A) •sorting type (optional) Specifies the type to use, when comparing elements. SORT_REGULAR Compare elements normally SORT_NUMERIC Compare elements as numeric values SORT_STRING Compare elements as string values array2 (Optional) Specifies an array array3 (Optional) Specifies an array Monica Deshmane(H.V.Desai college,Pune) 22
  • 23. Sorting multiple array at once All array size should be the same. $sub = array("C", "Java", "CPP", "PHP"); $author = array("Richie", "Sun", "Bjarne", "Orelly"); $price = array(300, 500, 400, 700); $b = array_multisort($sub, SORT_ASC, $author, $price); print_r($sub); print_r($author); print_r($price); Output: Array ( [0] => C [1] => CPP [2] => Java [3] => PHP ) Array ( [0] =>Bjarne [1]=>Orelly [2]=>Richie [3]=>Sun ) Array ( [0] => 300 [1] => 400 [2] => 500 [3] => 700 ) Monica Deshmane(H.V.Desai college,Pune) 23
  • 24. Revise…. Indexed- Sort() Rsort() Usort() Associative- Values- Asort() Arsort() Uasort() Kays- Ksort() Krsort() Uksort() Alphanumeric values- Natsort() Natcasesort() Multiple array sort- Array_multisort() Monica Deshmane(H.V.Desai college,Pune) 24