# How sort an array in PHP

View: 788    Dowload: 0   Comment: 0   Post by: hanhga   Category: Forum PHPBB, VBB   Fields: Other

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:

\$flowers = array("rose", "daisy" , "orchid", "tulip", "camomile");
sort(\$flowers);

for (\$i=0; \$i <= 4; \$i++) //prints the array elements
echo \$flowers[\$i]."
";

?>

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:

\$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]."
";

?>

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".

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:

foreach(\$shop as \$key => \$value)
echo \$key." costs ".\$value." dollars
";
//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:

\$shop = array ( "rose" => "5.00",
"daisy" => "4.00",
"orchid" => "2.00",
);
ksort(\$shop);

foreach(\$shop as \$key => \$value)
echo \$key." costs ".\$value." dollars
";
//print the array elements
?>

Output will be the following:

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

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

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

Posted on 15-12-2015

#### Comment:

To comment you must be logged in members.

## Files with category

• ### Document Management System in VB.Net

Category: Forum PHPBB, VBB     Fields: none

This is a Client Server project entitled Document Management System written in Vb.net and SQL Server Management Studio R2 2008 database for Mines and Geosciences Bureau . There are three (3) sections in the Mines and Geosciences Bureau, namely: under...

• ### Input Validation Using Filter Functions with php

Category: Forum PHPBB, VBB     Fields: Other

Filter functions in PHP might not be sexy, but they can improve the stability, security, and even maintainability of your code if you learn how to use them correctly.

• ### The Open/Closed Principle in PHP

Category: Forum PHPBB, VBB     Fields: Other

As usual, a good way to grasp what’s actually behind the curtain of the principle and how to take advantage of its benefits is by example. In this article I’ll be setting up a few approachable ones in an attempt to unveil the principle’s driving...

• ### Dependency Injection with Pimple in PHP

Category: Forum PHPBB, VBB     Fields: Other

In application development, we try to create independent modules so that we can reuse code in future projects. But, it’s difficult to create completely independent modules which provide useful functionality; their dependencies can cause maintenance...

• ### Working with Directories in PHP

Category: Forum PHPBB, VBB     Fields: Other

Let's start with simple listing the contents of a directory. We need three functions to perform this task: opendir(), readdir() and closedir(). The opendir() function takes one parameter, which is the directory we want to read, and returns a...

• ### Guide to Email (Text / HTML / Attachments) in PHP

Category: Forum PHPBB, VBB     Fields: Other

At first let's consider how to send a simple text email messages. PHP includes the mail() function for sending email, which takes three basic and two optional parameters.

• ### Dynamic Image Generation in PHP

Category: Forum PHPBB, VBB     Fields: Other

At first, have a look at the following script that creates captcha image. Next, we will consider each step of image generation in details.

• ### Form Processing with PHP

Category: Forum PHPBB, VBB     Fields: Other

Before you can process the information, you need to create an HTML form that will send information to your PHP script. There are two methods for sending data: POST and GET. These two types of sending information are defined in your HTML form...

File suggestion for you
• Php&mySql - 2852 view This Human Resource Management System PHP MySQL Source Codes is done using the programming language Asp.net and the data source SQL Server. This...

• WordPress template - 730 view SKT Magazine is a superfine free news- magazine WordPress theme ideal for online newspaper, publishing reviews, and editorial style websites. It is...

• Php&mySql - 468 view In part one, we covered the basics of Phake and demonstrated ways of executing tasks with it, covering groups, dependencies, and arguments. In this...

• Mobile applications - 5312 view We'll introduce a few concepts without going into too much detail. We'll look at: Basic file access, Clearing the screen, Drawing images, Using a...

• iPhone OS - 768 view In this tutorial, I am going to introduce you to tvOS development by creating a basic tvOS application.

Mega tags
• Php&mySql - 529 view If you’re already deep in PHP, then some of the stuff in this section...

• Form - Instructions - 775 view A. But factors have been shown to affect the reliability of the...

• References - 943 view Android Database TutorialAndroid Database ToolsThe first tool is...

• Php&mySql - 407 view In this post, we’ll go through the process of getting it up and...

• Php&mySql - 389 view Simply put, failing fast and loudly means that when an error occurs,...

• Autodesk 3DS Max - 677 view You'll start by creating a simple proxy model of a minigun, then move...

• Form - Instructions - 3281 view Ever had the need to block all other forms and controls when a...

• WordPress template - 478 view Today, codetitle released Musica – a nice magazine wordpress theme....

• Javascript - 555 view In spring mvc form submit tutorial, we learned about displaying...

• Flash template, Flash game - 1087 view Share Flash Template Music Dance intro