Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,693 questions

57,001 answers

573 users

How to use sort functions to sort an array in PHP

8 Answers

0 votes
$arr = array("ccc", "aaaaaaaaaaaaaaa", "ddddddd", "b");

sort($arr);

print_r($arr);

 
/*
run:
 
Array ( [0] => aaaaaaaaaaaaaaa [1] => b [2] => ccc [3] => ddddddd ) 
 
*/

 



answered Nov 17, 2015 by avibootz
edited Nov 17, 2015 by avibootz
0 votes
$arr = array(7, 3, 1, 2, 4);

sort($arr);

print_r($arr);

 
/*
run:
 
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 7 ) 
 
*/

 



answered Nov 17, 2015 by avibootz
0 votes
$arr = array(7, 3, 1, 2, 4);

rsort($arr);

print_r($arr);

 
/*
run:
 
Array ( [0] => 7 [1] => 4 [2] => 3 [3] => 2 [4] => 1 ) 
 
*/

 



answered Nov 17, 2015 by avibootz
0 votes
$arr = array("ccc", "aaaaaaaaaaaaaaa", "ddddddd", "b");

rsort($arr);

print_r($arr);

 
/*
run:
 
Array ( [0] => ddddddd [1] => ccc [2] => b [3] => aaaaaaaaaaaaaaa ) 
 
*/

 



answered Nov 17, 2015 by avibootz
0 votes
$arr = array("C"=>"95", "C++"=>"85", "PHP"=>"75");

asort($arr); // Ascending Order by Value

print_r($arr);

 
/*
run:
 
Array ( [PHP] => 75 [C++] => 85 [C] => 95 ) 
 
*/

 



answered Nov 17, 2015 by avibootz
0 votes
$arr = array("C"=>"95", "C++"=>"85", "PHP"=>"75");

ksort($arr); // Ascending Order by Key 

print_r($arr);

 
/*
run:
 
Array ( [C] => 95 [C++] => 85 [PHP] => 75 ) 
 
*/

 



answered Nov 17, 2015 by avibootz
0 votes
$arr = array("C"=>"95", "C++"=>"85", "PHP"=>"75");

arsort($arr); // Descending Order by Value 

print_r($arr);

 
/*
run:
 
Array ( [C] => 95 [C++] => 85 [PHP] => 75 )
 
*/

 



answered Nov 17, 2015 by avibootz
0 votes
$arr = array("C"=>"95", "C++"=>"85", "PHP"=>"75");

krsort($arr); // Descending Order by Key 

print_r($arr);

 
/*
run:
 
Array ( [PHP] => 75 [C++] => 85 [C] => 95 ) 
 
*/

 



answered Nov 17, 2015 by avibootz

Related questions

1 answer 167 views
167 views asked Sep 13, 2024 by avibootz
1 answer 258 views
258 views asked Oct 13, 2016 by avibootz
3 answers 397 views
2 answers 1,447 views
...