Jan 23, 2021, 06:15 AM

## News:

Choose a design and let our professionals help you build a successful website   - ITAcumens

## How to Sort an Array

Started by VelMurugan, May 20, 2009, 10:30 PM

Go Down

#### VelMurugan

##### May 20, 2009, 10:30 PM
How to Sort an Array

It is often necessary to arrange the elements in an array in numerical order from highest to lowest values (descending order) or vice versa (ascending order). If the array contains string values, alphabetical order may be needed. Sorting a one-dimensional arrays is quite easy.

* Sorting numerically indexed arrays

* Sorting associative arrays

* Sorting arrays in the reverse order

Sorting Numerically Indexed Arrays

At first we will consider an array which contains string values. The code below sorts array elements in ascending alphabetical order:

Code: [Select]
`<?php\$flowers = array("rose", "daisy" , "orchid", "tulip", "camomile");sort(\$flowers);for (\$i=0; \$i <= 4; \$i++) //prints the array elements    echo \$flowers[\$i]."<br \>";?>`

Now array elements will go in the alphabetical order. Output will be the following:

camomile
daisy
orchid
rose
tulip

We can sort values by numerical order too. If we have an array containing the prices of  flowers, we can sort it into ascending numeric order. Have a look at the example:

Code: [Select]
`<?php\$prices = array(1.25, 0.75 , 1.15, 1.00, 0.50);sort(\$prices);for (\$i=0; \$i <= 4; \$i++) //prints the array elements    echo \$prices[\$i]."<br \>";?>`

The prices will now be in the following order:

0.50
0.75
1.00
1.15
1.25

Note that the sort function is case sensitive, i.e. all capital letters come before all lowercase letters. So "A" is less than "Z", but "Z" is less than "a".

#### VelMurugan

#1
##### May 20, 2009, 10:30 PM
Sorting Associative Arrays

If we are using an associative array we cannot sort an array by using the sort() function. If we apply the sort() function on an associative array, it is sorted by the numeric value of the index. To sort an associative array we need to use the asort() function to keep keys and values together as they are sorted.

The following code creates an associative array containing the three flowers and their associated prices, and then sorts the array into ascending price order:

Code: [Select]
`<?php\$shop = array ( "rose" => "5.00",                "daisy" => "4.00",                "orchid" => "2.00",              ); asort(\$shop);foreach(\$shop as \$key => \$value)  echo \$key." costs ".\$value." dollars<br />";  //print the array elements?>`

The above example will output:

orchid costs 2.00 dollars
daisy costs 4.00 dollars
rose costs 5.00 dollars

The asort() function orders the array according to the value of each element. In the array, the values are the prices and the keys are the names of the flowers.

If instead of sorting by price we want to sort by flower name, we use ksort() function to sort an associative array according to the key.

The following code will result in the keys of the array being ordered alphabetically:

Code: [Select]
`<?php\$shop = array ( "rose" => "5.00",                "daisy" => "4.00",                "orchid" => "2.00",              ); ksort(\$shop);foreach(\$shop as \$key => \$value)  echo \$key." costs ".\$value." dollars<br />";  //print the array elements?>`

Output will be the following:

daisy costs 4.00 dollars
orchid costs 2.00 dollars
rose costs 5.00 dollars

#### VelMurugan

#2
##### May 20, 2009, 10:30 PM
Sorting Arrays in the Reverse Order

We discussed sort(), asort(), and  ksort() functions. All these functions sort array in the ascending order. Each of them has corresponding function that sorts an array in the descending order. These reverse functions are called rsort(), arsort() and krsort() respectively.

Reverse sorting functions are used the same way as usual sorting functions. Rsort() function sorts one-dimensional numerically indexed array by the values in reverse order.  Arsort() function sorts one-dimensional associative array by the values in reverse order. Krsort() function sorts one-dimensional associative array by the keys in reverse order.

Go Up

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it's been approved by a moderator.
Name:
Email:
Verification: