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

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

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

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,943 questions

51,883 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 90 views
90 views asked Sep 13, 2024 by avibootz
1 answer 168 views
168 views asked Oct 13, 2016 by avibootz
3 answers 243 views
2 answers 1,306 views
...