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

PPTX
PHP Functions & Arrays
Henry Osborne
 
PPT
JavaScript - An Introduction
Manvendra Singh
 
PPT
Php mysql ppt
Karmatechnologies Pvt. Ltd.
 
PDF
Php array
Nikul Shah
 
PPTX
SQL - DML and DDL Commands
Shrija Madhu
 
PPTX
Statements and Conditions in PHP
Maruf Abdullah (Rion)
 
PDF
Python Dictionary
Soba Arjun
 
PDF
Basics of JavaScript
Bala Narayanan
 
PHP Functions & Arrays
Henry Osborne
 
JavaScript - An Introduction
Manvendra Singh
 
Php array
Nikul Shah
 
SQL - DML and DDL Commands
Shrija Madhu
 
Statements and Conditions in PHP
Maruf Abdullah (Rion)
 
Python Dictionary
Soba Arjun
 
Basics of JavaScript
Bala Narayanan
 

What's hot (20)

PPT
Introduction to JavaScript
Andres Baravalle
 
PPTX
Basic Graphics in Java
Prakash Kumar
 
PPTX
Database Connectivity in PHP
Taha Malampatti
 
PDF
Files and streams
Pranali Chaudhari
 
PDF
Bottom up parser
Akshaya Arunan
 
PPTX
Packages in PL/SQL
Pooja Dixit
 
PDF
Sql tutorial
Rumman Ansari
 
PPSX
Javascript variables and datatypes
Varun C M
 
DOCX
SQL Tutorial for BCA-2
Raj vardhan
 
PDF
JavaScript - Chapter 13 - Browser Object Model(BOM)
WebStackAcademy
 
PPTX
STRINGS IN PYTHON
TanushTM1
 
PPT
Php forms
Anne Lee
 
PPTX
Operator precedance parsing
sanchi29
 
PPTX
Garbage collection
Somya Bagai
 
PPTX
Constructor and Destructor
Sunipa Bera
 
PPT
PHP - Introduction to Object Oriented Programming with PHP
Vibrant Technologies & Computers
 
PPTX
Static Data Members and Member Functions
MOHIT AGARWAL
 
PDF
JavaScript - Chapter 8 - Objects
WebStackAcademy
 
Introduction to JavaScript
Andres Baravalle
 
Basic Graphics in Java
Prakash Kumar
 
Database Connectivity in PHP
Taha Malampatti
 
Files and streams
Pranali Chaudhari
 
Bottom up parser
Akshaya Arunan
 
Packages in PL/SQL
Pooja Dixit
 
Sql tutorial
Rumman Ansari
 
Javascript variables and datatypes
Varun C M
 
SQL Tutorial for BCA-2
Raj vardhan
 
JavaScript - Chapter 13 - Browser Object Model(BOM)
WebStackAcademy
 
STRINGS IN PYTHON
TanushTM1
 
Php forms
Anne Lee
 
Operator precedance parsing
sanchi29
 
Garbage collection
Somya Bagai
 
Constructor and Destructor
Sunipa Bera
 
PHP - Introduction to Object Oriented Programming with PHP
Vibrant Technologies & Computers
 
Static Data Members and Member Functions
MOHIT AGARWAL
 
JavaScript - Chapter 8 - Objects
WebStackAcademy
 
Ad

Viewers also liked (6)

PPT
Functions in php
Mudasir Syed
 
PDF
Php tutorial
Niit
 
PDF
Php Tutorials for Beginners
Vineet Kumar Saini
 
PPT
Class 3 - PHP Functions
Ahmed Swilam
 
PDF
Abstract Class and Interface in PHP
Vineet Kumar Saini
 
PDF
Functions in PHP
Vineet Kumar Saini
 
Functions in php
Mudasir Syed
 
Php tutorial
Niit
 
Php Tutorials for Beginners
Vineet Kumar Saini
 
Class 3 - PHP Functions
Ahmed Swilam
 
Abstract Class and Interface in PHP
Vineet Kumar Saini
 
Functions in PHP
Vineet Kumar Saini
 
Ad

Similar to Sorting arrays in PHP (20)

PPTX
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
Dhivyaa C.R
 
PPTX
UNIT IV (4).pptx
DrDhivyaaCRAssistant
 
PPTX
Chapter 2 wbp.pptx
40NehaPagariya
 
PPT
Arrays in php
Laiby Thomas
 
PPTX
Marcs (bio)perl course
BITS
 
KEY
Intermediate PHP
Bradley Holt
 
PPTX
3160713_WP_GTU_Study_Material_Presentations_Unit-5_17042022102823PM.pptx
jignasha15
 
PPTX
Arrays syntax and it's functions in php.pptx
NikhilVij6
 
PPTX
Array functions for all languages prog.pptx
Asmi309059
 
PPTX
Array functions using php programming language.pptx
NikhilVij6
 
PPTX
Introduction to PHP Lecture 1
Ajay Khatri
 
PDF
Arrays in PHP
Vineet Kumar Saini
 
PPTX
Java script advance-auroskills (2)
BoneyGawande
 
PPTX
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
ZahouAmel1
 
PPTX
Unit 2-Arrays.pptx
mythili213835
 
PPT
PHP-04-Arrays.ppt
Leandro660423
 
PPT
PHP and MySQL with snapshots
richambra
 
PDF
Implode & Explode in PHP
Vineet Kumar Saini
 
PDF
WordCamp Portland 2018: PHP for WordPress
Alena Holligan
 
PDF
Array String - Web Programming
Amirul Azhar
 
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
Dhivyaa C.R
 
UNIT IV (4).pptx
DrDhivyaaCRAssistant
 
Chapter 2 wbp.pptx
40NehaPagariya
 
Arrays in php
Laiby Thomas
 
Marcs (bio)perl course
BITS
 
Intermediate PHP
Bradley Holt
 
3160713_WP_GTU_Study_Material_Presentations_Unit-5_17042022102823PM.pptx
jignasha15
 
Arrays syntax and it's functions in php.pptx
NikhilVij6
 
Array functions for all languages prog.pptx
Asmi309059
 
Array functions using php programming language.pptx
NikhilVij6
 
Introduction to PHP Lecture 1
Ajay Khatri
 
Arrays in PHP
Vineet Kumar Saini
 
Java script advance-auroskills (2)
BoneyGawande
 
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
ZahouAmel1
 
Unit 2-Arrays.pptx
mythili213835
 
PHP-04-Arrays.ppt
Leandro660423
 
PHP and MySQL with snapshots
richambra
 
Implode & Explode in PHP
Vineet Kumar Saini
 
WordCamp Portland 2018: PHP for WordPress
Alena Holligan
 
Array String - Web Programming
Amirul Azhar
 

More from Vineet Kumar Saini (20)

PDF
Add edit delete in Codeigniter in PHP
Vineet Kumar Saini
 
PDF
Introduction to Html
Vineet Kumar Saini
 
PDF
Computer Fundamentals
Vineet Kumar Saini
 
PDF
Country State City Dropdown in PHP
Vineet Kumar Saini
 
PDF
Pagination in PHP
Vineet Kumar Saini
 
PDF
Stripe in php
Vineet Kumar Saini
 
PDF
Install Drupal on Wamp Server
Vineet Kumar Saini
 
PDF
Joomla 2.5 Tutorial For Beginner PDF
Vineet Kumar Saini
 
PDF
Dropdown List in PHP
Vineet Kumar Saini
 
PDF
Update statement in PHP
Vineet Kumar Saini
 
PDF
Delete statement in PHP
Vineet Kumar Saini
 
PDF
Types of Error in PHP
Vineet Kumar Saini
 
PDF
GET and POST in PHP
Vineet Kumar Saini
 
PDF
Database connectivity in PHP
Vineet Kumar Saini
 
PDF
Programming in C
Vineet Kumar Saini
 
PDF
Browser information in PHP
Vineet Kumar Saini
 
PDF
Operators in PHP
Vineet Kumar Saini
 
PDF
Variables in PHP
Vineet Kumar Saini
 
PDF
MVC in PHP
Vineet Kumar Saini
 
PDF
SQL Limit in PHP
Vineet Kumar Saini
 
Add edit delete in Codeigniter in PHP
Vineet Kumar Saini
 
Introduction to Html
Vineet Kumar Saini
 
Computer Fundamentals
Vineet Kumar Saini
 
Country State City Dropdown in PHP
Vineet Kumar Saini
 
Pagination in PHP
Vineet Kumar Saini
 
Stripe in php
Vineet Kumar Saini
 
Install Drupal on Wamp Server
Vineet Kumar Saini
 
Joomla 2.5 Tutorial For Beginner PDF
Vineet Kumar Saini
 
Dropdown List in PHP
Vineet Kumar Saini
 
Update statement in PHP
Vineet Kumar Saini
 
Delete statement in PHP
Vineet Kumar Saini
 
Types of Error in PHP
Vineet Kumar Saini
 
GET and POST in PHP
Vineet Kumar Saini
 
Database connectivity in PHP
Vineet Kumar Saini
 
Programming in C
Vineet Kumar Saini
 
Browser information in PHP
Vineet Kumar Saini
 
Operators in PHP
Vineet Kumar Saini
 
Variables in PHP
Vineet Kumar Saini
 
MVC in PHP
Vineet Kumar Saini
 
SQL Limit in PHP
Vineet Kumar Saini
 

Recently uploaded (20)

PPTX
Open Quiz Monsoon Mind Game Final Set.pptx
Sourav Kr Podder
 
PPTX
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
mansk2
 
PPTX
NOI Hackathon - Summer Edition - GreenThumber.pptx
MartinaBurlando1
 
PDF
Landforms and landscapes data surprise preview
jpinnuck
 
PDF
The Final Stretch: How to Release a Game and Not Die in the Process.
Marta Fijak
 
PDF
3.The-Rise-of-the-Marathas.pdfppt/pdf/8th class social science Exploring Soci...
Sandeep Swamy
 
PDF
Mga Unang Hakbang Tungo Sa Tao by Joe Vibar Nero.pdf
MariellaTBesana
 
PPTX
Congenital Hypothyroidism pptx
AneetaSharma15
 
PDF
Sunset Boulevard Student Revision Booklet
jpinnuck
 
DOCX
UPPER GASTRO INTESTINAL DISORDER.docx
BANDITA PATRA
 
PPTX
Cardiovascular Pharmacology for pharmacy students.pptx
TumwineRobert
 
PDF
5.Universal-Franchise-and-Indias-Electoral-System.pdfppt/pdf/8th class social...
Sandeep Swamy
 
PDF
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
AshifaRamadhani
 
PDF
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Miraj Khan
 
PPTX
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
PPTX
IMMUNIZATION PROGRAMME pptx
AneetaSharma15
 
PPTX
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
PPTX
vedic maths in python:unleasing ancient wisdom with modern code
mistrymuskan14
 
PPTX
ACUTE NASOPHARYNGITIS. pptx
AneetaSharma15
 
Open Quiz Monsoon Mind Game Final Set.pptx
Sourav Kr Podder
 
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
Week 4 Term 3 Study Techniques revisited.pptx
mansk2
 
NOI Hackathon - Summer Edition - GreenThumber.pptx
MartinaBurlando1
 
Landforms and landscapes data surprise preview
jpinnuck
 
The Final Stretch: How to Release a Game and Not Die in the Process.
Marta Fijak
 
3.The-Rise-of-the-Marathas.pdfppt/pdf/8th class social science Exploring Soci...
Sandeep Swamy
 
Mga Unang Hakbang Tungo Sa Tao by Joe Vibar Nero.pdf
MariellaTBesana
 
Congenital Hypothyroidism pptx
AneetaSharma15
 
Sunset Boulevard Student Revision Booklet
jpinnuck
 
UPPER GASTRO INTESTINAL DISORDER.docx
BANDITA PATRA
 
Cardiovascular Pharmacology for pharmacy students.pptx
TumwineRobert
 
5.Universal-Franchise-and-Indias-Electoral-System.pdfppt/pdf/8th class social...
Sandeep Swamy
 
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
AshifaRamadhani
 
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Miraj Khan
 
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
IMMUNIZATION PROGRAMME pptx
AneetaSharma15
 
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
vedic maths in python:unleasing ancient wisdom with modern code
mistrymuskan14
 
ACUTE NASOPHARYNGITIS. pptx
AneetaSharma15
 

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.