How to Check an Array is Sorted or Not in PHP?

Given an array, the task is to check whether the given array is sorted or not. Arrays are often used to store and manipulate data. One common task is to check if an array is sorted in ascending or descending order. This article will explore different approaches to determine whether an array is sorted.

There are two main types of sorting orders to check:

  • Ascending order: Arrange the array elements from the smallest to the largest value, where each subsequent element is greater than or equal to the previous one.
  • Descending order: Arrange the array elements from the largest to the smallest value, where each subsequent element is less than or equal to the previous one.

These are the following approaches:

Table of Content

  • Iterative Comparison
  • Using sort and rsort functions

Iterative Comparison

The simplest way to check if an array is sorted is to iterate through the array and compare each element with the next one.

Example 1: Checking for Ascending Order.

PHP
<?php
  function isArrSorted($arr) {
      $n = count($arr);
      for ($i = 0; $i < $n - 1; $i++) {
          if ($arr[$i] > $arr[$i + 1]) {
              return false;
          }
      }
      return true;
  }
  // Driver Code
  $arr = [1, 2, 3, 4, 5];

  if(isArrSorted($arr)) {
      echo "Sorted Array in Ascending Order.";
  } else {
      echo "Unsorted Array.";
  }
?>

Output
Sorted Array in Ascending Order.

Example 2: Checking for Descending Order.

PHP
<?php
    function isArrSorted($arr) {
        $n = count($arr);
        
        for ($i = 0; $i < $n - 1; $i++) {
            if ($arr[$i] < $arr[$i + 1]) {
                return false;
            }
        }        
        return true;
    }
    // Driver Code
    $arr = [5, 4, 3, 2, 1];

    if(isArrSorted($arr)) {
        echo "Sorted Array in Descending Order.";
    } else {
        echo "Unsorted Array.";
    }
?>

Output
Sorted Array in Descending Order.

Using sort and rsort functions

Another approach is to sort a copy of the array and compare it with the original array. we will use sort() function to sort the array and then we will compare that to the original array to check whether

Example 1: Checking for Ascending Order using sort() function.

PHP
<?php
    function isArrSorted($arr) {
        $sortArr = $arr;
        sort($sortArr);
        return $arr === $sortArr;
    }
    // Driver Code
    $arr = [1, 2, 3, 4, 5];

    if(isArrSorted($arr)) {
        echo "Sorted Array in Ascending Order.";
    } else {
        echo "Unsorted Array.";
    }
?>

Output
Sorted Array in Ascending Order.

Example 2: Checking for Descending Order by using rsort() function.

PHP
<?php
    function isArrSorted($arr) {
        $sortArr = $arr;
        rsort($sortArr);
        return $arr === $sortArr;
    }
    // Driver Code
    $arr = [5, 4, 3, 2, 1];
    if(isArrSorted($arr)) {
        echo "Sorted Array in Descending Order.";
    } else {
        echo "Unsorted Array.";
    }
?>

Output
Sorted Array in Descending Order.