SlideShare a Scribd company logo
1 of 5
Download to read offline
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.

More Related Content

What's hot (20)

PHP variables
PHP  variablesPHP  variables
PHP variables
 
Arrays in PHP
Arrays in PHPArrays in PHP
Arrays in PHP
 
javascript objects
javascript objectsjavascript objects
javascript objects
 
Python programming : Arrays
Python programming : ArraysPython programming : Arrays
Python programming : Arrays
 
Javascript operators
Javascript operatorsJavascript operators
Javascript operators
 
Statements and Conditions in PHP
Statements and Conditions in PHPStatements and Conditions in PHP
Statements and Conditions in PHP
 
Javascript conditional statements
Javascript conditional statementsJavascript conditional statements
Javascript conditional statements
 
Php and MySQL
Php and MySQLPhp and MySQL
Php and MySQL
 
Php Using Arrays
Php Using ArraysPhp Using Arrays
Php Using Arrays
 
php
phpphp
php
 
Php introduction
Php introductionPhp introduction
Php introduction
 
Javascript arrays
Javascript arraysJavascript arrays
Javascript arrays
 
Javascript variables and datatypes
Javascript variables and datatypesJavascript variables and datatypes
Javascript variables and datatypes
 
PHP - Introduction to PHP Fundamentals
PHP -  Introduction to PHP FundamentalsPHP -  Introduction to PHP Fundamentals
PHP - Introduction to PHP Fundamentals
 
Get method and post method
Get method and post methodGet method and post method
Get method and post method
 
Data types in php
Data types in phpData types in php
Data types in php
 
Identifiers
Identifiers Identifiers
Identifiers
 
JavaScript - Chapter 10 - Strings and Arrays
 JavaScript - Chapter 10 - Strings and Arrays JavaScript - Chapter 10 - Strings and Arrays
JavaScript - Chapter 10 - Strings and Arrays
 
Chapter 02 php basic syntax
Chapter 02   php basic syntaxChapter 02   php basic syntax
Chapter 02 php basic syntax
 
Javascript
JavascriptJavascript
Javascript
 

Viewers also liked

Php tutorial
Php tutorialPhp tutorial
Php tutorialNiit
 
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
 

Viewers also liked (6)

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
 
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
 

Similar to 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
 

Similar to Sorting arrays in PHP (20)

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
 
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
 
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
 
Training on php by cyber security infotech (csi)
Training on  php by cyber security infotech (csi)Training on  php by cyber security infotech (csi)
Training on php by cyber security infotech (csi)
 

More from 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
 
Operators in PHP
Operators in PHPOperators in PHP
Operators 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
 

Recently uploaded

Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 

Recently uploaded (20)

Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 

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.