0PricingLogin
PHP Academy · Lesson

Sorting and Searching Arrays

Use sort, usort, array_search, and in_array.

Why Sort?

PHP provides a rich set of sorting and searching functions for arrays. Choose the right one based on whether you want to preserve keys, sort by value or key, or use a custom comparator.

sort() and rsort()

sort() sorts by value ascending (re-indexes); rsort() sorts descending:

<?php
$nums = [3, 1, 4, 1, 5, 9, 2, 6];
sort($nums);
print_r($nums); // [1, 1, 2, 3, 4, 5, 6, 9]

$letters = ['c', 'a', 'b'];
rsort($letters);
print_r($letters); // ['c', 'b', 'a']

All lessons in this course

  1. Indexed and Associative Arrays
  2. Multidimensional Arrays
  3. Sorting and Searching Arrays
  4. Array Transformation Functions
← Back to PHP Academy