Ad Code

Ticker

6/recent/ticker-posts

how to sort array in php

Sort PHP Array

There are some predefined php function for array sorting. These function perform different type sorting for the array:-

Function name Sorts by Order of sort Related functions
array_multisort() value first array or sort options array_walk()
asort() value low to high arsort()
arsort() value high to low asort()
krsort() key high to low ksort()
ksort() key low to high asort()
natcasesort() value natural, case insensitive natsort()
natsort() value natural natcasesort()
rsort() value high to low sort()
shuffle() value random array_rand()
sort() value low to high rsort()
uasort() value user defined uksort()
uksort() key user defined uasort()
usort() value user defined uasort()

Examples of these functions:-

1. array_multisort()

    This function used many types and sort indexed array, associative array, and multiple arrays at a time. Normally it sort in ascending order, see the example:-
$data=array("A","D","H","B","Z");
array_multisort($data);
print_r($data);
//output:-
Array ( [0] => A [1] => B [2] => D [3] => H [4] => Z )

2. asort()

This function sort according to the value of all types array and it is sort in ascending order of array values, see the example:-

$agedata=array("amit"=>"45","dinesh"=>"55","ramu"=>"25");
asort($agedata);

//output:-

Array ( [ramu] => 25 [amit] => 45 [2] => D [dinesh] => 55 [4] => Z )

Post a Comment

5 Comments

  1. Nice and good article.. it is very useful for me to learn and understand easily.. thanks for sharing your valuable information and time.. please keep updating.more 
    php jobs in hyderabad.

    ReplyDelete
  2. Great blog created by you. I read your blog, its best and useful information. You have done a great work. Super blogging and keep it up.php jobs in hyderabad.

    ReplyDelete
  3. Great Explanation and more useful for anyone.Thanks for sharing...
    PHP Training course in chennai

    ReplyDelete
  4. Nice blog has been shared by you. it will be really helpful to many peoples who are all working under the technology.thank you for sharing this blog.
    Digital Marketing Training In India

    ReplyDelete

Ad Code