Array Sorting Functions in PHP Practice Questions with Solutions

Introduction

PHP provides several built-in functions to sort arrays in ascending order, descending order, alphabetically, and by values or keys. These functions are useful when organizing employee records, product prices, student marks, and sales data. In this chapter, you will practise important sorting functions such as sort(), rsort(), asort(), arsort(), ksort(), krsort(), usort(), and uasort(). Array Sorting Functions in PHP Practice Questions with Solutions to help you to understand the concepts.

Question 1: Sort Numbers in Ascending Order Using sort()

Problem Statement

Create an array containing five numbers and arrange them from smallest to largest using the sort() function.

PHP Solution

<?php
$numbers = [45, 12, 89, 23, 56];

sort($numbers);

print_r($numbers);
?>

Sample Output

Array
(
    [0] => 12
    [1] => 23
    [2] => 45
    [3] => 56
    [4] => 89
)

Explanation

The sort() function arranges array elements in ascending order. It also resets the numeric indexes starting from 0.

Concepts Covered

  • sort() function
  • Ascending order
  • Numeric array sorting

Question 2: Sort Numbers in Descending Order Using rsort()

Problem Statement

Create an array of product prices and arrange them from highest to lowest using the rsort() function.

PHP Solution

<?php
$prices = [1500, 450, 2200, 800, 1200];

rsort($prices);

print_r($prices);
?>

Sample Output

Array
(
    [0] => 2200
    [1] => 1500
    [2] => 1200
    [3] => 800
    [4] => 450
)

Explanation

The rsort() function sorts array values in descending order. It also resets the numeric indexes.

Concepts Covered

  • rsort() function
  • Descending order
  • Product price sorting

Question 3: Sort Employee Salaries in Ascending Order Using asort()

Problem Statement

Create an associative array containing employee names and salaries. Sort the salaries from lowest to highest while keeping their employee names connected to the correct values.

PHP Solution

<?php
$salaries = [
    "Rahul" => 35000,
    "Priya" => 28000,
    "Amit" => 42000,
    "Neha" => 30000
];

asort($salaries);

print_r($salaries);
?>

Sample Output

Array
(
    [Priya] => 28000
    [Neha] => 30000
    [Rahul] => 35000
    [Amit] => 42000
)

Explanation

The asort() function sorts an associative array by its values in ascending order. It preserves the original keys, so each salary remains connected to the correct employee.

Concepts Covered

  • asort() function
  • Associative array sorting
  • Ascending value sorting
  • Preserving array keys

Question 4: Sort Employee Salaries in Descending Order Using arsort()

Problem Statement

Create an associative array containing employee names and salaries. Display employees from the highest salary to the lowest using arsort().

PHP Solution

<?php
$salaries = [
    "Rahul" => 35000,
    "Priya" => 28000,
    "Amit" => 42000,
    "Neha" => 30000
];

arsort($salaries);

print_r($salaries);
?>

Sample Output

Array
(
    [Amit] => 42000
    [Rahul] => 35000
    [Neha] => 30000
    [Priya] => 28000
)

Explanation

The arsort() function sorts an associative array by values in descending order. It preserves the original keys, keeping each employee connected to their salary.

Concepts Covered

  • arsort() function
  • Descending value sorting
  • Associative arrays
  • Preserving keys

Question 5: Sort an Associative Array by Keys Using ksort()

Problem Statement

Create an associative array containing department names and budgets. Arrange the department names alphabetically using the ksort() function.

PHP Solution

<?php
$departments = [
    "Sales" => 50000,
    "HR" => 30000,
    "Finance" => 45000,
    "IT" => 60000
];

ksort($departments);

print_r($departments);
?>

Sample Output

Array
(
    [Finance] => 45000
    [HR] => 30000
    [IT] => 60000
    [Sales] => 50000
)

Explanation

The ksort() function sorts an associative array according to its keys in ascending order. Here, department names are arranged alphabetically, while their budget values remain connected to them.

Concepts Covered

  • ksort() function
  • Sorting by keys
  • Alphabetical sorting
  • Associative arrays

Question 6: Sort an Associative Array by Keys in Descending Order Using krsort()

Problem Statement

Create an associative array containing product categories and their stock quantities. Arrange the category names in reverse alphabetical order using krsort().

PHP Solution

<?php
$stock = [
    "Electronics" => 50,
    "Clothing" => 120,
    "Furniture" => 30,
    "Books" => 200
];

krsort($stock);

print_r($stock);
?>

Sample Output

Array
(
    [Furniture] => 30
    [Electronics] => 50
    [Clothing] => 120
    [Books] => 200
)

Explanation

The krsort() function sorts an associative array by its keys in descending order. The category names appear in reverse alphabetical order, and their stock quantities remain attached to the correct keys.

Concepts Covered

  • krsort() function
  • Descending key sorting
  • Associative arrays

Question 7: Sort Student Marks Using usort()

Problem Statement

Create an array containing student marks. Sort the marks in ascending order using the usort() function and a custom comparison function.

PHP Solution

<?php
$marks = [78, 45, 92, 65, 88];

usort($marks, function ($a, $b) {
    return $a <=> $b;
});

print_r($marks);
?>

Sample Output

Array
(
    [0] => 45
    [1] => 65
    [2] => 78
    [3] => 88
    [4] => 92
)

Explanation

The usort() function sorts an array using a custom comparison function. The spaceship operator (<=>) compares two values and returns a result that determines their sorting order. Unlike asort(), usort() resets the array’s numeric indexes.

Concepts Covered

  • usort() function
  • Custom comparison function
  • Spaceship operator
  • Ascending sorting

Question 8: Sort Employees by Salary Using uasort()

Problem Statement

Create an associative array containing employee names and salaries. Sort employees by salary in ascending order using uasort() while preserving their names as keys.

PHP Solution

<?php
$employees = [
    "Rahul" => 35000,
    "Priya" => 28000,
    "Amit" => 42000,
    "Neha" => 30000
];

uasort($employees, function ($a, $b) {
    return $a <=> $b;
});

print_r($employees);
?>

Sample Output

Array
(
    [Priya] => 28000
    [Neha] => 30000
    [Rahul] => 35000
    [Amit] => 42000
)

Explanation

The uasort() function sorts an associative array by values using a custom comparison function. Unlike usort(), it preserves the original keys. This is useful when sorting employee records without losing employee names.

Concepts Covered

  • uasort() function
  • Custom comparison
  • Associative array sorting
  • Preserving keys

Question 9: Sort Product Names Alphabetically Using sort()

Problem Statement

Create an array containing product names in random order. Arrange the names alphabetically using the sort() function.

PHP Solution

<?php
$products = ["Monitor", "Keyboard", "Laptop", "Mouse", "Printer"];

sort($products);

print_r($products);
?>

Sample Output

Array
(
    [0] => Keyboard
    [1] => Laptop
    [2] => Monitor
    [3] => Mouse
    [4] => Printer
)

Explanation

The sort() function can also arrange string values in ascending alphabetical order. Here, the product names are sorted from A to Z.

Concepts Covered

  • String array sorting
  • sort() function
  • Alphabetical order

Question 10: Sort Numbers Using SORT_NUMERIC and SORT_STRING

Problem Statement

Create an array containing numeric values stored as strings. First, sort the values numerically and then sort them alphabetically as strings using the appropriate sorting flags.

PHP Solution

<?php
$numbers = ["100", "25", "5", "40"];

$numericNumbers = $numbers;
sort($numericNumbers, SORT_NUMERIC);

echo "Numeric Sorting:\n";
print_r($numericNumbers);

$stringNumbers = $numbers;
sort($stringNumbers, SORT_STRING);

echo "String Sorting:\n";
print_r($stringNumbers);
?>

Sample Output

Numeric Sorting:
Array
(
    [0] => 5
    [1] => 25
    [2] => 40
    [3] => 100
)

String Sorting:
Array
(
    [0] => 100
    [1] => 25
    [2] => 40
    [3] => 5
)

Explanation

The SORT_NUMERIC flag compares values as numbers, so 5 comes before 25 and 100. The SORT_STRING flag compares values as strings, so sorting follows character order. This distinction matters when numeric-looking values are stored as strings.

Concepts Covered

  • sort() function
  • SORT_NUMERIC
  • SORT_STRING
  • Numeric and string comparison

Key Takeaways

  • sort() arranges indexed array values in ascending order and resets numeric keys.
  • rsort() arranges indexed array values in descending order and resets numeric keys.
  • asort() sorts associative arrays by values in ascending order while preserving keys.
  • arsort() sorts associative arrays by values in descending order while preserving keys.
  • ksort() sorts associative arrays by keys in ascending order.
  • krsort() sorts associative arrays by keys in descending order.
  • usort() sorts values using a custom comparison function and resets numeric keys.
  • uasort() sorts values using a custom comparison function while preserving keys.
  • SORT_NUMERIC and SORT_STRING control how values are compared.
  • Sorting functions modify the original array.

Frequently Asked Questions (FAQs)

1. What are array sorting functions in PHP?

Array sorting functions arrange array elements in a particular order, such as ascending, descending, alphabetical, or based on custom conditions.

2. What is the difference between sort() and rsort()?

sort() arranges values in ascending order, while rsort() arranges values in descending order. Both reset numeric indexes.

3. What is the difference between asort() and arsort()?

asort() sorts an associative array by values in ascending order, while arsort() sorts values in descending order. Both preserve the original keys.

4. How do you sort an associative array by its keys?

Use ksort() for ascending key order and krsort() for descending key order.

5. What is the difference between usort() and uasort()?

Both functions use a custom comparison function. usort() resets numeric indexes, whereas uasort() preserves the original array keys.

6. Can we sort strings alphabetically in PHP?

Yes. The sort() function can arrange string values alphabetically in ascending order.

7. What does SORT_NUMERIC do in PHP?

SORT_NUMERIC tells PHP to compare array values numerically instead of comparing them as strings.

8. What does SORT_STRING do in PHP?

SORT_STRING tells PHP to compare array values as strings, using string comparison order.

9. Do PHP sorting functions modify the original array?

Yes. The sorting functions covered in this chapter modify the array passed to them. Make a copy first if you need to keep the original order.

10. Which PHP sorting function should be used to sort employee salaries while keeping employee names?

Use asort() for ordinary ascending value sorting or arsort() for descending value sorting. Use uasort() when you need a custom comparison rule.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top