web directoryearn money in seconds   Free file hosting, free video sharing

<<< The Web Hosting >>>


Go Back   Noeman GSM > Webhosting & Website development > Website development > Programming

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-30-2007, 10:59 AM
Spiral's Avatar
CO-Admin
 
Join Date: Sep 2004
Location: Martil
Age: 25
Posts: 15,829
Reputation: 19992
Spiral Mega Super MemberSpiral Mega Super MemberSpiral Mega Super MemberSpiral Mega Super MemberSpiral Mega Super MemberSpiral Mega Super MemberSpiral Mega Super MemberSpiral Mega Super MemberSpiral Mega Super MemberSpiral Mega Super MemberSpiral Mega Super Member
Default 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 Code:
<?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 Code:
<?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 Code:
<?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 Code:
<?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 Code:
<?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 Code:
<?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
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-17-2007, 07:32 PM
Membre
 
Join Date: Aug 2007
Posts: 10
Reputation: 10
j4jambo is on a distinguished road
Default

good stuff guyz
Reply With Quote
  #3 (permalink)  
Old 08-17-2007, 07:34 PM
Membre
 
Join Date: Aug 2007
Posts: 10
Reputation: 10
j4jambo is on a distinguished road
Default

keep doing good works
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 01:25 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
Copyright © 2004-2008 Noeman . All rights reserved
Problem Mortgage | Car Parts | Life Insurance Policy | Ringtones | Loans

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112