How to Check an Array is Sorted or Not in PHP?
Last Updated :
29 May, 2024
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:
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.";
}
?>
OutputSorted 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.";
}
?>
OutputSorted 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.";
}
?>
OutputSorted 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.";
}
?>
OutputSorted Array in Descending Order.
Similar Reads
How to check an element is exists in array or not in PHP ? An array may contain elements belonging to different data types, integer, character, or logical type. The values can then be inspected in the array using various in-built methods : Approach 1 (Using in_array() method): The array() method can be used to declare an array. The in_array() method in PHP
2 min read
How to get elements in reverse order of an array in PHP ? An array is a collection of elements stored together. Every element in an array belong to a similar data type. The elements in the array are recognized by their index values. The elements can be subjected to a variety of operations, including reversal. There are various ways to reverse the elements
4 min read
Sort an array of dates in PHP We are given an array of multiple dates in (Y-m-d) format. We have to write a program in PHP to sort all the dates in the array in decreasing order. Examples : Input : array("2018-06-04", "2014-06-08", "2018-06-05") Output : 2018-06-05 2018-06-04 2014-06-08 Input : array("2016-09-12", "2009-09-08",
2 min read
How to find the index of an element in an array using PHP ? In this article, we will discuss how to find the index of an element in an array in PHP. Array indexing starts from 0 to n-1. Here we have some common approachesTable of ContentUsing array_search() FunctionUsing array_flip()Using a custom functionUsing a foreach LoopUsing array_keys FunctionUsing ar
6 min read
Find the Second Largest Element in an Array in PHP We will be given with an integer array, i.e. $array1 or $ array2, and the task is to find the second largest element in the array. There are multiple ways to approach and solve this problem however using sorting is the most common and concise approach as we use the rsort() function for further opera
3 min read