You want to sort an array in a specific way.
Use sort() to sort an array using the traditional definition of sort. It doesn't preserve the key/value association between elements; instead, entries are reindexed starting at 0 and going upward.
To sort numerically, pass SORT_NUMERIC as the second argument to sort().
Use rsort() to sort the array in reverse order.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="EN" lang="EN"> <head> <title>PHP</title> <meta Name="Author" Content="Hann So"> </head> <body> <p> <?php $fruits = array('Apples', 'Grapes', 'Bananas', 'Oranges'); print_r($fruits); sort($fruits); echo "<p><b>Use sort() to sort the array.</b></p>"; print_r($fruits); rsort($fruits); echo "<p><b>Use rsort() to sort the array in reverse order.</b></p>"; print_r($fruits); // Sort numerically echo "<p><b>This is an array with numbers.</b></p>"; $scores = array(1,10,2,20,3,30); print_r($scores); sort($scores, SORT_NUMERIC); echo "<p><b>Sort numerically with SORT_NUMERIC as argument.</b></p>"; print_r($scores); rsort($scores, SORT_NUMERIC); echo "<p><b>Sort numerically in reverse order.</b></p>"; print_r($scores); echo "<p><b>This is an associative array.</b></p>"; $fruits2 = array('red'=>'Apples', 'green'=>'Grapes', 'yellow'=>'Bananas', 'orange'=>'Oranges'); print_r($fruits2); sort($fruits2); echo "<p><b>Use sort() to sort the associative array.</b></p>"; print_r($fruits2); ?> </p> </body> </html> |
To preserve the key/value links, use asort(). Use arsort() to sort in reverse order.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="EN" lang="EN"> <head> <title>PHP</title> <meta Name="Author" Content="Hann So"> </head> <body> <p> <?php $fruits = array('Apples', 'Grapes', 'Bananas', 'Oranges'); print_r($fruits); asort($fruits); echo "<p><b>Use asort() to sort by keeping the key/value.</b></p>"; print_r($fruits); arsort($fruits); echo "<p><b>Use arsort() to sort the array in reverse order.</b></p>"; print_r($fruits); // Sort numerically echo "<p><b>This is an array with numbers.</b></p>"; $scores = array(1,10,2,20,3,30); print_r($scores); asort($scores, SORT_NUMERIC); echo "<p><b>Sort numerically with SORT_NUMERIC as argument.</b></p>"; print_r($scores); arsort($scores, SORT_NUMERIC); echo "<p><b>Sort numerically in reverse order.</b></p>"; print_r($scores); echo "<p><b>This is an associative array.</b></p>"; $fruits2 = array('red'=>'Apples', 'green'=>'Grapes', 'yellow'=>'Bananas', 'orange'=>'Oranges'); print_r($fruits2); asort($fruits2); echo "<p><b>Use asort() to sort the associative array by keeping the key/value.</b></p>"; print_r($fruits2); ?> </p> </body> </html> |
Use natsort() to sort the array using a natural sorting algorithm. Under natural sorting, you can mix strings and numbers inside the elements and still get the right answer.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="EN" lang="EN"> <head> <title>PHP</title> <meta Name="Author" Content="Hann So"> </head> <body> <p> <?php $fruits = array('Apples20', 'Apples1', 'Apples10', 'Apples5'); print_r($fruits); natsort($fruits); echo "<p><b>Use natsort() to natural sort.</b></p>"; print_r($fruits); echo "<p><b>This is an associative array.</b></p>"; $fruits2 = array('red'=>'Apples20', 'green'=>'Apples1', 'yellow'=>'Apples10', 'orange'=>'Apples5'); print_r($fruits2); natsort($fruits2); echo "<p><b>Use natsort() to sort the associative array.</b></p>"; print_r($fruits2); ?> </p> </body> </html> |
Use ksort() to sort the array based on keys and not values. Use krsort() to sort by reverse by keys.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="EN" lang="EN"> <head> <title>PHP</title> <meta Name="Author" Content="Hann So"> </head> <body> <p> <?php $fruits = array(4=>'Apples', 2=>'Grapes', 'Bananas', 'Oranges'); print_r($fruits); ksort($fruits); echo "<p><b>Use ksort() to sort by keys.</b></p>"; print_r($fruits); krsort($fruits); echo "<p><b>Use krsort() to sort the array in reverse order.</b></p>"; print_r($fruits); echo "<p><b>This is an associative array.</b></p>"; $fruits2 = array('red'=>'Apples', 'green'=>'Grapes', 'yellow'=>'Bananas', 'orange'=>'Oranges'); print_r($fruits2); ksort($fruits2); echo "<p><b>Use ksort() to sort the associative array by keys.</b></p>"; print_r($fruits2); krsort($fruits2); echo "<p><b>Use krsort() to sort the associative array in reverse order.</b></p>"; print_r($fruits2); ?> </p> </body> </html> |
To define your own sorting routine, use usort() in combination with a custom comparison function.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="EN" lang="EN"> <head> <title>PHP</title> <meta Name="Author" Content="Hann So"> </head> <body> <p> <?php // sort in natural order // strnatcmp() returns a value greater than 0 if $a>$b, // 0 if $a=$b, and less than 0 if $a<$b function natrsort($a, $b) { return strnatcmp($a, $b); } $fruits = array('Apples20', 'Apples1', 'Apples10', 'Apples5'); print_r($fruits); usort($fruits, 'natrsort'); echo "<p><b>Use usort() to customize sort.</b></p>"; print_r($fruits); </p> </body> </html> |