frame

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In Register

PHP Sorting Arrays

PHP Sorting Arrays

sort() - Sort array in ascending alphabetical order

<!DOCTYPE html>
<html>
<body>


<?php
$cars = array("Volvo", "BMW", "Toyota");
sort($cars);


$clength = count($cars);
for($x = 0; $x < $clength; $x++) {
  echo $cars[$x];
  echo "<br>";
}
?>


</body>
</html>

sort() - Sort array in ascending numerical order

<!DOCTYPE html>
<html>
<body>


<?php
$numbers = array(4, 6, 2, 22, 11);
sort($numbers);


$arrlength = count($numbers);
for($x = 0; $x < $arrlength; $x++) {
  echo $numbers[$x];
  echo "<br>";
}
?>


</body>
</html>


rsort() - Sort array in descending alphabetical order

<!DOCTYPE html>
<html>
<body>


<?php
$cars = array("Volvo", "BMW", "Toyota");
rsort($cars);


$clength = count($cars);
for($x = 0; $x < $clength; $x++) {
  echo $cars[$x];
  echo "<br>";
}
?>


</body>
</html>


rsort() - Sort array in descending numerical order

<!DOCTYPE html>
<html>
<body>


<?php
$numbers = array(4, 6, 2, 22, 11);
rsort($numbers);


$arrlength = count($numbers);
for($x = 0; $x < $arrlength; $x++) {
  echo $numbers[$x];
  echo "<br>";
}
?>


</body>
</html>

asort() - Sort array in ascending order, according to value

<!DOCTYPE html>
<html>
<body>


<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
asort($age);


foreach($age as $x => $x_value) {
  echo "Key=" . $x . ", Value=" . $x_value;
  echo "<br>";
}
?>


</body>
</html>


ksort() - Sort array in ascending order, according to key

<!DOCTYPE html>
<html>
<body>


<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
ksort($age);


foreach($age as $x => $x_value) {
  echo "Key=" . $x . ", Value=" . $x_value;
  echo "<br>";
}
?>


</body>
</html>


arsort() - Sort array in descending order, according to value

<!DOCTYPE html>
<html>
<body>


<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
arsort($age);


foreach($age as $x => $x_value) {
  echo "Key=" . $x . ", Value=" . $x_value;
  echo "<br>";
}
?>


</body>
</html>

krsort() - Sort array in descending order, according to key

<!DOCTYPE html>
<html>
<body>


<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
krsort($age);


foreach($age as $x => $x_value) {
  echo "Key=" . $x . ", Value=" . $x_value;
  echo "<br>";
}
?>


</body>
</html>


Tagged:
Sign In or Register to comment.

Developer

| make vanilla clean and light
@ 2016 Your Sitename, All rights reserved. Material Design is a design language developed by Google. Material Design makes more liberal use of grid-based layouts and responsive animations and transitions.
Powered by VanillaForums, Designed by ThemeSteam

Contact us

info@example.com
support@example.com
(800) 3032120

Get In Touch