How sort an array in PHP

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

9 point/3 review File has been tested

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

How sort an array in PHP

How sort an array in PHP Posted on 15-12-2015  At first we will consider an array which contains string values. The code below sorts array elements in ascending alphabetical order: 3/10 502

Comment:

To comment you must be logged in members.

Files with category

  • Input Validation Using Filter Functions with php

    Input Validation Using Filter Functions with php

    View: 490    Download: 0   Comment: 0

    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

    The Open/Closed Principle in PHP

    View: 371    Download: 0   Comment: 0

    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

    Dependency Injection with Pimple in PHP

    View: 418    Download: 0   Comment: 0

    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

    Working with Directories in PHP

    View: 557    Download: 0   Comment: 0

    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

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

    View: 524    Download: 0   Comment: 0

    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

    Dynamic Image Generation in PHP

    View: 441    Download: 0   Comment: 0

    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

    Form Processing with PHP

    View: 755    Download: 0   Comment: 0

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

  • Function in PHP

    Function in PHP

    View: 638    Download: 0   Comment: 0

    Category: Forum PHPBB, VBB     Fields: Other

    A function is a block of code which can be called from any point in a script after it has been declared. It is basically a compartmentalized PHP script designed to accomplish a single task. Furthermore, code contained within functions is ignored...

 
File suggestion for you
File top downloads
Codetitle.net - library source code to share, download the file to the community
Copyright © 2015. All rights reserved. codetitle.net Develope by Vinagon .Ltd