★ wanayoo — archive 1999 http://www.php.net/manual/function.sort.phpNouvelle recherche | Portail wanayoo

PHP Home Page

Manual Table of Contents
Up to Arrays
Quick Reference
German version of this pageJapanese version of this pageItalian version of this pageFrench version of this pageHungarian version of this page
Arrays
* array
* array_count_values
* array_diff
* array_flip
* array_intersect
* array_keys
* array_merge
* array_merge_recursive
* array_multisort
* array_pad
* array_pop
* array_push
* array_rand
* array_reverse
* array_shift
* array_slice
* array_splice
* array_unique
* array_unshift
* array_values
* array_walk
* arsort
* asort
* compact
* count
* current
* each
* end
* extract
* in_array
* key
* krsort
* ksort
* list
* next
* pos
* prev
* range
* reset
* rsort
* shuffle
* sizeof
* sort
* uasort
* uksort
* usort
Manual: sort
View the source code for this pageSearch the site



Previous page
 sizeof
 Updated
Wed, 09 Aug 2000
uasort 
Next page


sort

(PHP3 , PHP4 )

sort -- Sort an array

Description

void sort (array array [, int sort_flags])

This function sorts an array. Elements will be arranged from lowest to highest when this function has completed.

Example 1. Sort() example


$fruits = array ("lemon", "orange", "banana", "apple");
sort ($fruits);
reset ($fruits);
while (list ($key, $val) = each ($fruits)) {
    echo "$key -> $val\n";
}
      

This example would display:


fruits[0] = apple
fruits[1] = banana
fruits[2] = lemon
fruits[3] = orange
      

The fruits have been sorted in alphabetical order.

The optional second parameter sort_flags may be used to modify the sorting behavior using theese valies:

Sorting type flags:

  • SORT_REGULAR - compare items normally

  • SORT_NUMERIC - compare items numerically

  • SORT_STRING - compare items as strings

See also: arsort(), asort(), ksort(), rsort(), and usort().


User Contributed Notes: sort


muell@delirium.ch
17-Jun-1999 03:10
The example code is not gonna do what you'd expect it to do. Yet, there's an easy work-around:

$fruits = array("lemon","orange","banana","apple");
sort($fruits);
while (list($key,$val) = each($fruits) ) {
echo "fruits[$key] => $val
";
}



mbuehler@centralnet.ch
31-Aug-1999 07:47
The example above does'nt work when I tried it. I used following commands:

for(reset($fruits); $key <= key($fruits); next($fruits))
{
$key = key($fruits);
echo "fruits[$key] = ".$fruits[$key]."
";
}

Markus Buehler



yann@algonet.se
09-Sep-1999 09:02
see also uksort()


guttorm@mauimail.com
04-Dec-1999 03:45
The for statement (if it is like ANSI-C) should work as follows:
for the statements a, b, c, and d:
for ( a ; b ; c )
d;
means:
a;
while(b)
{
d;
c;
}
The example-code, assumes it differently.
When I searched the quick ref about for, it found nothing, by the way.



misillet@tin.it
17-Mar-2000 10:58
Note that using sort() it seems to sort by ASCII code, because "AC" is before "Ab" in the result array



jwf3167@saintjoe.edu
21-Mar-2000 12:11
i have a 2-D array which is the equivalent of 3 arrays side by side inside it. what i want to do is sort 1 of the arrays, and keep the data in the other 2 all lined up with the first because they go with it. can anyone tell me how to go about that?

$data[0][x] is a list of song titles
$data[1][x] is a list of the corresponding artists
$data[2][x] is a list of the corresponding lyrics

i'd like to alphabetize by artist. thanks.

-Justin Walters
<a href=/old?u=http%3A%2F%2Fwww.saintjoe.edu%2F~jwf3167%26gt%3Bwww.saintjoe.edu%2F~jwf3167%26lt%3B%2Fa%26gt%3B%3C%2Ftt&y=1999>


ultrafunkula@mad.scientist.com
30-Mar-2000 10:08
What you really want is asort(). The neat thing about PHP arrays is that even though they have a traditional numerical index, they are not defined by it. What I mean by this is if you define an array $data[], then $data[2] does not necessarily exist between $data[1] and $data[3]. So if you asort() by the artist name and then use each() to recover the array elements in the order they exist(rather than an incremental loop which ties you to their arbitrary numerical key), you can sort your data by any dimension of your array. Try this out: $data[0][1]='Title 2'; $data[0][2]='Title 1'; $data[0][0]='Title 3'; $data[1][1]='Barney'; $data[1][0]='Charlie'; $data[1][2]='Al'; $data[2][2]='Sing the song of Al'; $data[2][1]='Jam with Barney'; $data[2][0]='Charlie Rocks'; while (list($key) = each($data[1])) { print $data[0][$key].",".$data[1][$key].",".$data[2][$key]."

"; } asort($data[1]); while (list($key) = each($data[1])) { print $data[0][$key].",".$data[1][$key].",".$data[2][$key]."

"; } The interesting part is that the first, UNSORTED array doesn't come out in numerical order, because it wasn't put in that way. If you asort($data[2]) you'll sort by lyrics, and so on. Just make sure you remember to each() by the same dimension as you asort().



luie16@mailexcite.com
20-Apr-2000 08:05
I'm not sure if what I need to do is possible or not.. I have a two-dimensional array. I need to apply the rsort() function to the nested array ($votes) in order for the array to be sorted by largest number to smallest keeping in mind that both, $names and $votes need to stay in relation in order for the amount of votes to be assigned to the proper name. $totals[$names[$votes]]


 About Notes


Previous page
 sizeof
 Updated
Wed, 09 Aug 2000
uasort 
Next page





Who's responsible for this?
Top of this page

Site
Hosting:



Located in
United States
Elements of this website are subject to copyright.
Questions about installing or using PHP should be directed to one of the mailing lists.
Only questions about the website should be directed to webmaster@php.net.