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

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,089 questions

40,774 answers

573 users

How to use sort functions to sort an array in PHP

Learn & Practice SQL


222 views
asked Nov 17, 2015 by avibootz
edited Mar 10, 2016 by avibootz

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 96 views
96 views asked Oct 13, 2016 by avibootz
3 answers 134 views
2 answers 1,168 views
...