T4VN is an online PHP Help community that provides PHP Tutorials, PHP Examples, PHP Scripts, PHP Support
    HOME  |  HOSTING COUPON  |  TEMPLATE  |  PHP SCRIPTS  |  LINK TO US  |  LINK  |  REGISTER | CONTACT
::: Member Login :::
 Username
 Password
 
Forgot your password ?
::: PHP Tutotal :::
  PHP Basic (7)
  PHP Advanced (4)
  PHP Database (2)
  Coding Step By Step (8)
  PHP and AJAX (0)
::: PHP Example :::
  Arrays (1)
  Code Highlighters (3)
  Database Functions (12)
  Date & Time (5)
  E-Mail (6)
  Forms (5)
  Guestbooks (1)
  Logging (2)
  Miscellaneous (10)
  Password Generators (3)
  Randomizers (3)
  String Manipulation (10)
  User Authentication (6)
::: Search On T4VN :::
::: T4VN Statistics :::
  PHP Scripts : 61
  PHP Example : 67
  PHP Tutorials : 21
  PHP News : 93
  Total Coupon : 36
  Other Tutorials :
  Member : 209
  Visitor Online : 2
  Today Visit: 310
  Total Visitor : 262320
  Most Online : 31
::: Sponsored Links :::

PR 4 For This Webpage
Home Today :

Sorting an Array in PHP


Now and then you need to sort your arrays alphabetically or numerically, if nothing else, then just to apply some programming logic and attain the desired output. You can sort an array in PHP by using two functions: sort(), to sort an array in ascending order, and rsort(), to sort an array in the reverse order, or descending order. I’ll illustrate this function with an example.

First, we loop through an array without applying any sorting:

PHP Tutorials :

<?php

$narray
[0]="Alfred";
$narray[1]="Robert";
$narray[2]="Deepak";
$narray[3]="Teresa";
$narray[4]="Joshua";
$narray[5]="Chandni";
$narray[6]="Sadiq";
$narray[7]="Vladimir";

for(
$i=0$i<8$i++)
{
print 
$narray[$i] . "<br />";
}

?> 



The output of this PHP code is:

Alfred
Robert
Deepak
Teresa
Joshua
Chandni
Sadiq
Vladimir

Now we apply the sort() function and see what happens.

PHP Tutorials :

<?php

$narray
[0]="Alfred";
$narray[1]="Robert";
$narray[2]="Deepak";
$narray[3]="Teresa";
$narray[4]="Joshua";
$narray[5]="Chandni";
$narray[6]="Sadiq";
$narray[7]="Vladimir";

sort($narray);

for(
$i=0$i<8$i++)
{
print 
$narray[$i] . "<br />";
}

?> 



The output of this PHP code is:

Alfred
Chandni
Deepak
Joshua
Robert
Sadiq
Teresa
Vladimir

You can see that the names have been alphabetically sorted in the ascending order. To sort of the names in descending order, we change the program like this:


PHP Tutorials :

<?php

$narray
[0]="Alfred";
$narray[1]="Robert";
$narray[2]="Deepak";
$narray[3]="Teresa";
$narray[4]="Joshua";
$narray[5]="Chandni";
$narray[6]="Sadiq";
$narray[7]="Vladimir";

rsort($narray);

for(
$i=0$i<8$i++)
{
print 
$narray[$i] . "<br />";
}

?> 



Now the output is:

Vladimir
Teresa
Sadiq
Robert
Joshua
Deepak
Chandni
Alfred

A few days ago we studied about associative arrays in PHP. Now, you cannot sort an associative array by using the sort() function. Let’s see what happens if you apply the sort() function on an associative array in PHP:


PHP Tutorials :

<?php

$narray
["IBM"]="International Business Machines";
$narray["MS"]="Microsoft";
$narray["CA"]="Computer Associated";
$narray["WHO"]="World Health Organization";
$narray["UK"]="United Kingdon";
$narray["BA"]="Something Random";

sort($narray);

foreach(
$narray as $key => $value)
{
print 
$key " = " $value "<br />";
}

?> 


The outcome you get is:

0 = Computer Associated
1 = International Business Machines
2 = Microsoft
3 = Something Random
4 = United Kingdon
5 = World Health Organization

So you can see that if you apply the sort() function on an associative array, it is sorted by the numeric value of the index. To sort an associative array, you use the asort() function in the following manner:


PHP Tutorials :

<?php

$narray
["IBM"]="International Business Machines";
$narray["MS"]="Microsoft";
$narray["CA"]="Computer Associated";
$narray["WHO"]="World Health Organization";
$narray["UK"]="United Kingdon";
$narray["BA"]="Something Random";
asort($narray);
foreach(
$narray as $key => $value)
{
print 
$key " = " $value "<br />";
}

?> 



The result is:

CA = Computer Associated
IBM = International Business Machines
MS = Microsoft
BA = Something Random
UK = United Kingdon
WHO = World Health Organization

As you can see, the array has been sorted in the ascending order by the value of the array, and not the string index value, or the key of the array. You can use arsort() to sort an associative array in the descending order.

To sort an associative array according to the key of the array, you can use the ksort() function in the following manner:


PHP Tutorials :

<?php

$narray
["IBM"]="International Business Machines";
$narray["MS"]="Microsoft";
$narray["CA"]="Computer Associated";
$narray["WHO"]="World Health Organization";
$narray["UK"]="United Kingdon";
$narray["BA"]="Something Random";

ksort($narray);

foreach(
$narray as $key => $value)
{
print 
$key " = " $value "<br />";
}

?>



Similarly, you can sort an associative array according to the key, in ascending order by using the krsort() function.

Author : Amrit Hallan


Other Tutorials
Random Title Generator using Arrays
PHP Includes
Date and Time in PHP
Using If Else Statements

::: Resources :::
  Links Directory
  Programming 2 3
  Webmaster 2 3
  Web Design 2 3
  Web Hosting 2 3
  Other Links 2 3
  Asian ShowBiz News
  Teach Seo For You
::: New Templates :::




::: Other Tutorials :::
 Program Design

  Powered By T4VN.NET - Version 2.0 - CopyRight © T4VN.NET 2005-2007