Your SlideShare is downloading. ×
Sorting arrays in PHP
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Sorting arrays in PHP

3,482

Published on

Published in: Education, Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,482
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
69
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. PHP Tutorials By Vineet Kumar Saini Sorting Arrays in PHPIntroductionSorting 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 groupof 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 anarray by value.Some sorting functions are as follows:  sort()  asort()  rsort()  arsort()1. sort() functionUsing 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>OutputIn the above example we defined four elements (names) in the array. In this example weused the sort() function. The sort() function arranges the elements of an array inascending (alphabetically) order. You can see in the following image:
  • 2. PHP Tutorials By Vineet Kumar Saini2. asort() functionIn 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 indexposition. 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>OutputIn this output you will see the array sorted by their index position.
  • 3. PHP Tutorials By Vineet Kumar Saini3. rsort() functionThe rsort() function is used to sort an array in descending (alphabetic) order. An exampleof 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>OutputIn this output you will see the array sorted in a descending (alphabetically) order.
  • 4. PHP Tutorials By Vineet Kumar Saini4. arsort() functionThe arsort() function is a combination of asort() + rsort(). The arsort() function will sortan 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 SainiConclusionIn this article you saw how to sort an array in PHP. Using this article one can easilyunderstand sorting an arrays in PHP.

×