SlideShare ist ein Scribd-Unternehmen logo
1 von 5
Downloaden Sie, um offline zu lesen
PHP Tutorials By Vineet Kumar Saini

                            Sorting Arrays in PHP
Introduction

Sorting is a process by which we can arrange the elements of a list in a specific order i.e.
ascending or descending order. We can say sorting is the process of putting a list or a group
of items in a specific order. Sorting may be alphabetical or numerical.

In PHP, sorting is a process of arranging the elements of an array in a specific order i.e.
ascending or descending order. Using sorting you can analyze a list in a more effective way.
You can sort an array by value, key or randomly. In the most situations we need to sort an
array by value.

Some sorting functions are as follows:

      sort()
      asort()
      rsort()
      arsort()

1. sort() function

Using the sort() function you can sort easily and simply. The example of the sort()
function is as follows:

<html>
<body bgcolor="pink">
<h3>Sort() Function</h3>
<?php
   $name=array ("Vineet","Arjun","Manish","Laxman");
   echo "<b>Before sorting the values are:</b>".'<br>';
   print_r($name).'<br>'.'<br>';
   sort($name);
   echo"<br/>";
   echo"<br/>";
   echo "<b>After sorting values become:</b>".'<br>';
   print_r($name).'<br>';
?>
</body>
</html>

Output

In the above example we defined four elements (names) in the array. In this example we
used the sort() function. The sort() function arranges the elements of an array in
ascending (alphabetically) order. You can see in the following image:
PHP Tutorials By Vineet Kumar Saini




2. asort() function

In the example above you saw that the elements of the array are arranged in ascending
(alphabetically) order. The asort() function sorts an array and also maintains the index
position. An example of the asort() function is as follows:

<html>
<body bgcolor="pink">
<h3>Sort() Function</h3>
<?php
   $name=array ("Vineet","Arjun","Manish","Laxman");
   echo "<b>Before sorting the values are:</b>".'<br>';
   print_r($name).'<br>'.'<br>';
   asort($name);
   echo"<br/>";
   echo"<br/>";
   echo "<b>After sorting values become:</b>".'<br>';
   print_r($name).'<br>';
?>
</body>
</html>

Output

In this output you will see the array sorted by their index position.
PHP Tutorials By Vineet Kumar Saini




3. rsort() function

The rsort() function is used to sort an array in descending (alphabetic) order. An example
of the rsort() function is as follows:

<html>
<body bgcolor="pink">
<h3>Sort() Function</h3>
<?php
   $name=array ("Vineet","Arjun","Manish","Laxman");
   echo "<b>Before sorting the values are:</b>".'<br>';
   print_r($name).'<br>'.'<br>';
   rsort($name);
   echo"<br/>";
   echo"<br/>";
   echo "<b>After sorting values become:</b>".'<br>';
   print_r($name).'<br>';
?>
</body>
</html>

Output

In this output you will see the array sorted in a descending (alphabetically) order.
PHP Tutorials By Vineet Kumar Saini




4. arsort() function

The arsort() function is a combination of asort() + rsort(). The arsort() function will sort
an array in reverse order and maintain index position. The example of the arsort()
function is as follows:

<html>
<body bgcolor="pink">
<h3>Sort() Function</h3>
<?php
   $name=array ("Vineet","Arjun","Manish","Laxman");
   echo "<b>Before sorting the values are:</b>".'<br>';
   print_r($name).'<br>'.'<br>';
   arsort($name);
   echo"<br/>";
   echo"<br/>";
   echo "<b>After sorting values become:</b>".'<br>';
   print_r($name).'<br>';
?>
</body>
</html>

Output
PHP Tutorials By Vineet Kumar Saini




Conclusion

In this article you saw how to sort an array in PHP. Using this article one can easily
understand sorting an arrays in PHP.

Weitere ähnliche Inhalte

Was ist angesagt?

JavaScript - Chapter 11 - Events
 JavaScript - Chapter 11 - Events  JavaScript - Chapter 11 - Events
JavaScript - Chapter 11 - Events WebStackAcademy
 
Java collections concept
Java collections conceptJava collections concept
Java collections conceptkumar gaurav
 
Basic Crud In Django
Basic Crud In DjangoBasic Crud In Django
Basic Crud In Djangomcantelon
 
How to Create an Array & types in PHP
How to Create an Array & types in PHP How to Create an Array & types in PHP
How to Create an Array & types in PHP Ajit Sinha
 
XSS Attacks Exploiting XSS Filter by Masato Kinugawa - CODE BLUE 2015
XSS Attacks Exploiting XSS Filter by Masato Kinugawa - CODE BLUE 2015XSS Attacks Exploiting XSS Filter by Masato Kinugawa - CODE BLUE 2015
XSS Attacks Exploiting XSS Filter by Masato Kinugawa - CODE BLUE 2015CODE BLUE
 
JavaScript - Chapter 15 - Debugging Techniques
 JavaScript - Chapter 15 - Debugging Techniques JavaScript - Chapter 15 - Debugging Techniques
JavaScript - Chapter 15 - Debugging TechniquesWebStackAcademy
 
javaScript.ppt
javaScript.pptjavaScript.ppt
javaScript.pptsentayehu
 
Javascript Design Patterns
Javascript Design PatternsJavascript Design Patterns
Javascript Design PatternsLilia Sfaxi
 

Was ist angesagt? (20)

JavaScript - Chapter 11 - Events
 JavaScript - Chapter 11 - Events  JavaScript - Chapter 11 - Events
JavaScript - Chapter 11 - Events
 
Operators in PHP
Operators in PHPOperators in PHP
Operators in PHP
 
Java collections concept
Java collections conceptJava collections concept
Java collections concept
 
Php basics
Php basicsPhp basics
Php basics
 
Basic Crud In Django
Basic Crud In DjangoBasic Crud In Django
Basic Crud In Django
 
XSS
XSSXSS
XSS
 
Css pseudo-classes
Css pseudo-classesCss pseudo-classes
Css pseudo-classes
 
How to Create an Array & types in PHP
How to Create an Array & types in PHP How to Create an Array & types in PHP
How to Create an Array & types in PHP
 
File Uploading in PHP
File Uploading in PHPFile Uploading in PHP
File Uploading in PHP
 
Flask – Python
Flask – PythonFlask – Python
Flask – Python
 
Uploading a file with php
Uploading a file with phpUploading a file with php
Uploading a file with php
 
XSS Attacks Exploiting XSS Filter by Masato Kinugawa - CODE BLUE 2015
XSS Attacks Exploiting XSS Filter by Masato Kinugawa - CODE BLUE 2015XSS Attacks Exploiting XSS Filter by Masato Kinugawa - CODE BLUE 2015
XSS Attacks Exploiting XSS Filter by Masato Kinugawa - CODE BLUE 2015
 
Javascript
JavascriptJavascript
Javascript
 
Laravel ppt
Laravel pptLaravel ppt
Laravel ppt
 
Type casting
Type castingType casting
Type casting
 
Php technical presentation
Php technical presentationPhp technical presentation
Php technical presentation
 
JavaScript - Chapter 15 - Debugging Techniques
 JavaScript - Chapter 15 - Debugging Techniques JavaScript - Chapter 15 - Debugging Techniques
JavaScript - Chapter 15 - Debugging Techniques
 
javaScript.ppt
javaScript.pptjavaScript.ppt
javaScript.ppt
 
Javascript Design Patterns
Javascript Design PatternsJavascript Design Patterns
Javascript Design Patterns
 
PHP Security
PHP SecurityPHP Security
PHP Security
 

Andere mochten auch

Php tutorial
Php tutorialPhp tutorial
Php tutorialNiit
 
PHP Functions & Arrays
PHP Functions & ArraysPHP Functions & Arrays
PHP Functions & ArraysHenry Osborne
 
Class 3 - PHP Functions
Class 3 - PHP FunctionsClass 3 - PHP Functions
Class 3 - PHP FunctionsAhmed Swilam
 
Abstract Class and Interface in PHP
Abstract Class and Interface in PHPAbstract Class and Interface in PHP
Abstract Class and Interface in PHPVineet Kumar Saini
 

Andere mochten auch (7)

Functions in php
Functions in phpFunctions in php
Functions in php
 
Php tutorial
Php tutorialPhp tutorial
Php tutorial
 
Php Tutorials for Beginners
Php Tutorials for BeginnersPhp Tutorials for Beginners
Php Tutorials for Beginners
 
PHP Functions & Arrays
PHP Functions & ArraysPHP Functions & Arrays
PHP Functions & Arrays
 
Class 3 - PHP Functions
Class 3 - PHP FunctionsClass 3 - PHP Functions
Class 3 - PHP Functions
 
Abstract Class and Interface in PHP
Abstract Class and Interface in PHPAbstract Class and Interface in PHP
Abstract Class and Interface in PHP
 
Functions in PHP
Functions in PHPFunctions in PHP
Functions in PHP
 

Ähnlich wie Sorting arrays in PHP

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
 
Marcs (bio)perl course
Marcs (bio)perl courseMarcs (bio)perl course
Marcs (bio)perl courseBITS
 
Introduction to PHP Lecture 1
Introduction to PHP Lecture 1Introduction to PHP Lecture 1
Introduction to PHP Lecture 1Ajay Khatri
 
Java script advance-auroskills (2)
Java script advance-auroskills (2)Java script advance-auroskills (2)
Java script advance-auroskills (2)BoneyGawande
 
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvvLecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvvZahouAmel1
 
PHP and MySQL with snapshots
 PHP and MySQL with snapshots PHP and MySQL with snapshots
PHP and MySQL with snapshotsrichambra
 
WordCamp Portland 2018: PHP for WordPress
WordCamp Portland 2018: PHP for WordPressWordCamp Portland 2018: PHP for WordPress
WordCamp Portland 2018: PHP for WordPressAlena Holligan
 
Array String - Web Programming
Array String - Web ProgrammingArray String - Web Programming
Array String - Web ProgrammingAmirul Azhar
 
Laravel collections an overview - Laravel SP
Laravel collections an overview - Laravel SPLaravel collections an overview - Laravel SP
Laravel collections an overview - Laravel SPMatheus Marabesi
 
Php tips-and-tricks4128
Php tips-and-tricks4128Php tips-and-tricks4128
Php tips-and-tricks4128PrinceGuru MS
 

Ähnlich wie Sorting arrays in PHP (20)

UNIT IV (4).pptx
UNIT IV (4).pptxUNIT IV (4).pptx
UNIT IV (4).pptx
 
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 ...
 
Chapter 2 wbp.pptx
Chapter 2 wbp.pptxChapter 2 wbp.pptx
Chapter 2 wbp.pptx
 
Arrays in php
Arrays in phpArrays in php
Arrays in php
 
Marcs (bio)perl course
Marcs (bio)perl courseMarcs (bio)perl course
Marcs (bio)perl course
 
Intermediate PHP
Intermediate PHPIntermediate PHP
Intermediate PHP
 
Php array
Php arrayPhp array
Php array
 
Introduction to PHP Lecture 1
Introduction to PHP Lecture 1Introduction to PHP Lecture 1
Introduction to PHP Lecture 1
 
Arrays in PHP
Arrays in PHPArrays in PHP
Arrays in PHP
 
Java script advance-auroskills (2)
Java script advance-auroskills (2)Java script advance-auroskills (2)
Java script advance-auroskills (2)
 
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvvLecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
 
Unit 2-Arrays.pptx
Unit 2-Arrays.pptxUnit 2-Arrays.pptx
Unit 2-Arrays.pptx
 
PHP-04-Arrays.ppt
PHP-04-Arrays.pptPHP-04-Arrays.ppt
PHP-04-Arrays.ppt
 
PHP and MySQL with snapshots
 PHP and MySQL with snapshots PHP and MySQL with snapshots
PHP and MySQL with snapshots
 
Implode & Explode in PHP
Implode & Explode in PHPImplode & Explode in PHP
Implode & Explode in PHP
 
WordCamp Portland 2018: PHP for WordPress
WordCamp Portland 2018: PHP for WordPressWordCamp Portland 2018: PHP for WordPress
WordCamp Portland 2018: PHP for WordPress
 
Array String - Web Programming
Array String - Web ProgrammingArray String - Web Programming
Array String - Web Programming
 
Laravel collections an overview - Laravel SP
Laravel collections an overview - Laravel SPLaravel collections an overview - Laravel SP
Laravel collections an overview - Laravel SP
 
Php tips-and-tricks4128
Php tips-and-tricks4128Php tips-and-tricks4128
Php tips-and-tricks4128
 
Ruby Classes
Ruby ClassesRuby Classes
Ruby Classes
 

Mehr von Vineet Kumar Saini (20)

Add edit delete in Codeigniter in PHP
Add edit delete in Codeigniter in PHPAdd edit delete in Codeigniter in PHP
Add edit delete in Codeigniter in PHP
 
Introduction to Html
Introduction to HtmlIntroduction to Html
Introduction to Html
 
Computer Fundamentals
Computer FundamentalsComputer Fundamentals
Computer Fundamentals
 
Country State City Dropdown in PHP
Country State City Dropdown in PHPCountry State City Dropdown in PHP
Country State City Dropdown in PHP
 
Pagination in PHP
Pagination in PHPPagination in PHP
Pagination in PHP
 
Stripe in php
Stripe in phpStripe in php
Stripe in php
 
Install Drupal on Wamp Server
Install Drupal on Wamp ServerInstall Drupal on Wamp Server
Install Drupal on Wamp Server
 
Joomla 2.5 Tutorial For Beginner PDF
Joomla 2.5 Tutorial For Beginner PDFJoomla 2.5 Tutorial For Beginner PDF
Joomla 2.5 Tutorial For Beginner PDF
 
Dropdown List in PHP
Dropdown List in PHPDropdown List in PHP
Dropdown List in PHP
 
Update statement in PHP
Update statement in PHPUpdate statement in PHP
Update statement in PHP
 
Delete statement in PHP
Delete statement in PHPDelete statement in PHP
Delete statement in PHP
 
Types of Error in PHP
Types of Error in PHPTypes of Error in PHP
Types of Error in PHP
 
GET and POST in PHP
GET and POST in PHPGET and POST in PHP
GET and POST in PHP
 
Database connectivity in PHP
Database connectivity in PHPDatabase connectivity in PHP
Database connectivity in PHP
 
Programming in C
Programming in CProgramming in C
Programming in C
 
Browser information in PHP
Browser information in PHPBrowser information in PHP
Browser information in PHP
 
Variables in PHP
Variables in PHPVariables in PHP
Variables in PHP
 
MVC in PHP
MVC in PHPMVC in PHP
MVC in PHP
 
SQL Limit in PHP
SQL Limit in PHPSQL Limit in PHP
SQL Limit in PHP
 
Print function in PHP
Print function in PHPPrint function in PHP
Print function in PHP
 

Kürzlich hochgeladen

Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operationalssuser3e220a
 
Sulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesSulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesVijayaLaxmi84
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Projectjordimapav
 
4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptxmary850239
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...DhatriParmar
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...DhatriParmar
 
Using Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea DevelopmentUsing Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea Developmentchesterberbo7
 
ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6Vanessa Camilleri
 
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
Unraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptxUnraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptx
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptxDhatriParmar
 
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxMichelleTuguinay1
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmStan Meyer
 
Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1GloryAnnCastre1
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfPrerana Jadhav
 
4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptx4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptxmary850239
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 

Kürzlich hochgeladen (20)

Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operational
 
Sulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesSulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their uses
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Project
 
4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx
 
prashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Professionprashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Profession
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
 
Using Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea DevelopmentUsing Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea Development
 
ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6
 
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
Unraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptxUnraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptx
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
 
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and Film
 
Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdf
 
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of EngineeringFaculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
 
4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptx4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptx
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 

Sorting arrays in PHP

  • 1. PHP Tutorials By Vineet Kumar Saini Sorting Arrays in PHP Introduction Sorting is a process by which we can arrange the elements of a list in a specific order i.e. ascending or descending order. We can say sorting is the process of putting a list or a group of items in a specific order. Sorting may be alphabetical or numerical. In PHP, sorting is a process of arranging the elements of an array in a specific order i.e. ascending or descending order. Using sorting you can analyze a list in a more effective way. You can sort an array by value, key or randomly. In the most situations we need to sort an array by value. Some sorting functions are as follows:  sort()  asort()  rsort()  arsort() 1. sort() function Using the sort() function you can sort easily and simply. The example of the sort() function is as follows: <html> <body bgcolor="pink"> <h3>Sort() Function</h3> <?php $name=array ("Vineet","Arjun","Manish","Laxman"); echo "<b>Before sorting the values are:</b>".'<br>'; print_r($name).'<br>'.'<br>'; sort($name); echo"<br/>"; echo"<br/>"; echo "<b>After sorting values become:</b>".'<br>'; print_r($name).'<br>'; ?> </body> </html> Output In the above example we defined four elements (names) in the array. In this example we used the sort() function. The sort() function arranges the elements of an array in ascending (alphabetically) order. You can see in the following image:
  • 2. PHP Tutorials By Vineet Kumar Saini 2. asort() function In the example above you saw that the elements of the array are arranged in ascending (alphabetically) order. The asort() function sorts an array and also maintains the index position. An example of the asort() function is as follows: <html> <body bgcolor="pink"> <h3>Sort() Function</h3> <?php $name=array ("Vineet","Arjun","Manish","Laxman"); echo "<b>Before sorting the values are:</b>".'<br>'; print_r($name).'<br>'.'<br>'; asort($name); echo"<br/>"; echo"<br/>"; echo "<b>After sorting values become:</b>".'<br>'; print_r($name).'<br>'; ?> </body> </html> Output In this output you will see the array sorted by their index position.
  • 3. PHP Tutorials By Vineet Kumar Saini 3. rsort() function The rsort() function is used to sort an array in descending (alphabetic) order. An example of the rsort() function is as follows: <html> <body bgcolor="pink"> <h3>Sort() Function</h3> <?php $name=array ("Vineet","Arjun","Manish","Laxman"); echo "<b>Before sorting the values are:</b>".'<br>'; print_r($name).'<br>'.'<br>'; rsort($name); echo"<br/>"; echo"<br/>"; echo "<b>After sorting values become:</b>".'<br>'; print_r($name).'<br>'; ?> </body> </html> Output In this output you will see the array sorted in a descending (alphabetically) order.
  • 4. PHP Tutorials By Vineet Kumar Saini 4. arsort() function The arsort() function is a combination of asort() + rsort(). The arsort() function will sort an array in reverse order and maintain index position. The example of the arsort() function is as follows: <html> <body bgcolor="pink"> <h3>Sort() Function</h3> <?php $name=array ("Vineet","Arjun","Manish","Laxman"); echo "<b>Before sorting the values are:</b>".'<br>'; print_r($name).'<br>'.'<br>'; arsort($name); echo"<br/>"; echo"<br/>"; echo "<b>After sorting values become:</b>".'<br>'; print_r($name).'<br>'; ?> </body> </html> Output
  • 5. PHP Tutorials By Vineet Kumar Saini Conclusion In this article you saw how to sort an array in PHP. Using this article one can easily understand sorting an arrays in PHP.