SlideShare a Scribd company logo
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

More Related Content

PPTX
Chap 3php array part 3
monikadeshmane
 
PPTX
Python programming -Tuple and Set Data type
Megha V
 
PDF
Rcommands-for those who interested in R.
Dr. Volkan OBAN
 
PPTX
Python data structures
kalyanibedekar
 
PDF
Python fundamentals - basic | WeiYuan
Wei-Yuan Chang
 
PDF
Introduction to Python
UC San Diego
 
PDF
Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...
Matt Harrison
 
Chap 3php array part 3
monikadeshmane
 
Python programming -Tuple and Set Data type
Megha V
 
Rcommands-for those who interested in R.
Dr. Volkan OBAN
 
Python data structures
kalyanibedekar
 
Python fundamentals - basic | WeiYuan
Wei-Yuan Chang
 
Introduction to Python
UC San Diego
 
Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...
Matt Harrison
 

What's hot (20)

ODP
How to Become a Tree Hugger: Random Forests and Predictive Modeling for Devel...
Matt Harrison
 
PDF
Arrays in PHP
Vineet Kumar Saini
 
PDF
Python programming : List and tuples
Emertxe Information Technologies Pvt Ltd
 
PDF
Python-Tuples
Krishna Nanda
 
PDF
Probabilistic Programming in Scala
BeScala
 
PDF
Python Usage (5-minute-summary)
Ohgyun Ahn
 
PDF
Next Level Testing
James Saryerwinnie
 
PPTX
R programming
Pramodkumar Jha
 
PDF
Python_ 3 CheatSheet
Dr. Volkan OBAN
 
PPTX
Tuple in python
Sharath Ankrajegowda
 
PDF
Python Puzzlers - 2016 Edition
Nandan Sawant
 
PDF
Cheat sheet python3
sxw2k
 
PDF
iOS와 케라스의 만남
Mijeong Jeon
 
PDF
Fp java8
Yanai Franchi
 
PDF
Scala Parallel Collections
Aleksandar Prokopec
 
PPTX
List in Python
Sharath Ankrajegowda
 
PDF
Scala collections
Inphina Technologies
 
PPT
Phylogenetics in R
schamber
 
PDF
프알못의 Keras 사용기
Mijeong Jeon
 
PPTX
Pa1 session 3_slides
aiclub_slides
 
How to Become a Tree Hugger: Random Forests and Predictive Modeling for Devel...
Matt Harrison
 
Arrays in PHP
Vineet Kumar Saini
 
Python programming : List and tuples
Emertxe Information Technologies Pvt Ltd
 
Python-Tuples
Krishna Nanda
 
Probabilistic Programming in Scala
BeScala
 
Python Usage (5-minute-summary)
Ohgyun Ahn
 
Next Level Testing
James Saryerwinnie
 
R programming
Pramodkumar Jha
 
Python_ 3 CheatSheet
Dr. Volkan OBAN
 
Tuple in python
Sharath Ankrajegowda
 
Python Puzzlers - 2016 Edition
Nandan Sawant
 
Cheat sheet python3
sxw2k
 
iOS와 케라스의 만남
Mijeong Jeon
 
Fp java8
Yanai Franchi
 
Scala Parallel Collections
Aleksandar Prokopec
 
List in Python
Sharath Ankrajegowda
 
Scala collections
Inphina Technologies
 
Phylogenetics in R
schamber
 
프알못의 Keras 사용기
Mijeong Jeon
 
Pa1 session 3_slides
aiclub_slides
 
Ad

Similar to Chap 3php array part4 (20)

PDF
arrays.pdf
WaheedAnwar20
 
PPT
arrays.ppt
SchoolEducationDepar
 
PPT
arrays.ppt
SchoolEducationDepar
 
PDF
Sorting arrays in PHP
Vineet Kumar Saini
 
PPT
Class 4 - PHP Arrays
Ahmed Swilam
 
PPT
Arrays in php
Laiby Thomas
 
PDF
Php array
Nikul Shah
 
PPTX
Introduction to php 6
pctechnology
 
PPTX
UNIT IV (4).pptx
DrDhivyaaCRAssistant
 
PPTX
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
Dhivyaa C.R
 
PPTX
Web Application Development using PHP Chapter 4
Mohd Harris Ahmad Jaal
 
PPT
Php Chapter 2 3 Training
Chris Chubb
 
PPTX
Chap 3php array part 2
monikadeshmane
 
PPTX
Introduction to PHP Lecture 1
Ajay Khatri
 
PPT
PHP - Introduction to PHP Arrays
Vibrant Technologies & Computers
 
PPTX
PHP Functions & Arrays
Henry Osborne
 
PPTX
Array Methods.pptx
stargaming38
 
PPTX
Chap 3php array part1
monikadeshmane
 
PPTX
Arrays PHP 03
mohamedsaad24
 
PPT
Using arrays with PHP for forms and storing information
Nicole Ryan
 
arrays.pdf
WaheedAnwar20
 
Sorting arrays in PHP
Vineet Kumar Saini
 
Class 4 - PHP Arrays
Ahmed Swilam
 
Arrays in php
Laiby Thomas
 
Php array
Nikul Shah
 
Introduction to php 6
pctechnology
 
UNIT IV (4).pptx
DrDhivyaaCRAssistant
 
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
Dhivyaa C.R
 
Web Application Development using PHP Chapter 4
Mohd Harris Ahmad Jaal
 
Php Chapter 2 3 Training
Chris Chubb
 
Chap 3php array part 2
monikadeshmane
 
Introduction to PHP Lecture 1
Ajay Khatri
 
PHP - Introduction to PHP Arrays
Vibrant Technologies & Computers
 
PHP Functions & Arrays
Henry Osborne
 
Array Methods.pptx
stargaming38
 
Chap 3php array part1
monikadeshmane
 
Arrays PHP 03
mohamedsaad24
 
Using arrays with PHP for forms and storing information
Nicole Ryan
 
Ad

More from monikadeshmane (18)

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 3
monikadeshmane
 
PPTX
php string-part 2
monikadeshmane
 
PPTX
PHP string-part 1
monikadeshmane
 
PPTX
java script
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 3
monikadeshmane
 
php string-part 2
monikadeshmane
 
PHP string-part 1
monikadeshmane
 
java script
monikadeshmane
 
ip1clientserver model
monikadeshmane
 
Chap1introppt2php(finally done)
monikadeshmane
 
Chap1introppt1php basic
monikadeshmane
 

Recently uploaded (20)

PPTX
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
PPTX
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
DOCX
UPPER GASTRO INTESTINAL DISORDER.docx
BANDITA PATRA
 
PDF
Exploring-Forces 5.pdf/8th science curiosity/by sandeep swamy notes/ppt
Sandeep Swamy
 
PPTX
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
PDF
Types of Literary Text: Poetry and Prose
kaelandreabibit
 
PDF
Landforms and landscapes data surprise preview
jpinnuck
 
PDF
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Miraj Khan
 
PPTX
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
PPTX
IMMUNIZATION PROGRAMME pptx
AneetaSharma15
 
PDF
The Picture of Dorian Gray summary and depiction
opaliyahemel
 
PDF
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
Sandeep Swamy
 
DOCX
Action Plan_ARAL PROGRAM_ STAND ALONE SHS.docx
Levenmartlacuna1
 
PDF
7.Particulate-Nature-of-Matter.ppt/8th class science curiosity/by k sandeep s...
Sandeep Swamy
 
PDF
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
PPT
Python Programming Unit II Control Statements.ppt
CUO VEERANAN VEERANAN
 
PDF
Virat Kohli- the Pride of Indian cricket
kushpar147
 
PPTX
PREVENTIVE PEDIATRIC. pptx
AneetaSharma15
 
PPTX
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
PPTX
vedic maths in python:unleasing ancient wisdom with modern code
mistrymuskan14
 
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
UPPER GASTRO INTESTINAL DISORDER.docx
BANDITA PATRA
 
Exploring-Forces 5.pdf/8th science curiosity/by sandeep swamy notes/ppt
Sandeep Swamy
 
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
Types of Literary Text: Poetry and Prose
kaelandreabibit
 
Landforms and landscapes data surprise preview
jpinnuck
 
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Miraj Khan
 
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
IMMUNIZATION PROGRAMME pptx
AneetaSharma15
 
The Picture of Dorian Gray summary and depiction
opaliyahemel
 
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
Sandeep Swamy
 
Action Plan_ARAL PROGRAM_ STAND ALONE SHS.docx
Levenmartlacuna1
 
7.Particulate-Nature-of-Matter.ppt/8th class science curiosity/by k sandeep s...
Sandeep Swamy
 
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
Python Programming Unit II Control Statements.ppt
CUO VEERANAN VEERANAN
 
Virat Kohli- the Pride of Indian cricket
kushpar147
 
PREVENTIVE PEDIATRIC. pptx
AneetaSharma15
 
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
vedic maths in python:unleasing ancient wisdom with modern code
mistrymuskan14
 

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