PHP

PHP Menu

PHP

Sorting Arrays - PHP Basics

PHP has a variety of built-in functions for sorting array elements in various ways, such as alphabetically or numerically in ascending or decreasing order. The most commonly used PHP array sort functions are:

  • sort() - sort arrays in ascending order
  • rsort() - sort arrays in descending order
  • asort() - sort associative arrays in ascending order, according to the value
  • ksort() - sort associative arrays in ascending order, according to the key
  • arsort() - sort associative arrays in descending order, according to the value
  • krsort() - sort associative arrays in descending order, according to the key

The sort() function

The following example sorts the elements of the arrays in ascending order:

<?php
$countries = array("USA", "Germany", "UK", "Australia"); sort($countries); print_r($countries);
$myNum = array(141, 26, 324, 142, 251); sort($myNum); print_r($myNum);

The rsort() function

The following example sorts the elements of the $cars array in descending order:

<?php
$countries = array("USA", "Germany", "UK", "Australia"); rsort($countries); print_r($countries);
$myNum = array(141, 26, 324, 142, 251); rsort($myNum); print_r($myNum);

The asort() function

The asort() function sorts the elements of an associative array in ascending order according to the value. The following example sorts an associative array in ascending order, according to value.

<?php
$ages = array("Mark" => "35", "Jeff" => "37", "Ivan" => "43"); asort($ages); print_r($ages);

The arsort() function

The arsort() function sorts the elements of an associative array in descending order according to the value. The following example sorts an associative array in descending order, according to value.

<?php
$ages = array("Mark" => "35", "Jeff" => "37", "Ivan" => "43"); arsort($ages); print_r($ages);

The ksort() function

The ksort() function sorts the elements of an associative array in ascending order according to key. The following example sorts an associative array in ascending order, according to key.

<?php
$ages = array("Mark" => "35", "Jeff" => "37", "Ivan" => "43"); ksort($ages); print_r($ages);

The krsort() function

The krsort() function sorts the elements of an associative array in descending order according to key. The following example sorts an associative array in descending order, according to key.

<?php
$ages = array("Mark" => "35", "Jeff" => "37", "Ivan" => "43"); krsort($ages); print_r($ages);

Exercise

Create an array named $cars with the following values: ("Volvo", "Volkswagen", "Toyota", "Ford"). Sort the array in ascending order and print the values using the foreach loop.

<?php
$var = 123;
<?php
$cars = array("Volvo", "Volkswagen", "Toyota", "Ford"); sort($cars); foreach ($cars as $value) { echo "$value <br>"; };
{ "test_output_contains": [ { "expected":"Ford", "error_message":"Sorry. Please try again." }, { "expected":"Toyota", "error_message":"Sorry. Please try again." }, { "expected":"Volkswagen", "error_message":"Sorry. Please try again." }, { "expected":"Volvo", "error_message":"Sorry. Please try again." } ], "test_variable_exists": { "object":"$cars", "error_message":"Have you declared <code>$cars<\/code>?" }, "success_message":"Good job!", "error_message":"Please read the instructions again."}

Introduction

PHP Basics

PHP Advance

PHP OOP

PHP Functions and Methods