How to Check an Array is Sorted or Not in PHP? Last Updated : 29 May, 2024 Comments Improve Suggest changes Like Article Like Report 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 ComparisonUsing sort and rsort functionsIterative ComparisonThe 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 functionsAnother 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. Comment More infoAdvertise with us Next Article How to Check an Array is Sorted or Not in PHP? B blalverma92 Follow Improve Article Tags : PHP 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 Sort an Associative Array by Key in PHP Given an Associative Array, the task is to sort the associative array by its keys in PHP. There are different methods to sort Associative Array by keys, these are described below: Table of ContentUsing ksort() FunctionUsing uksort() FunctionConverting to a Regular Array for SortingUsing array_multis 3 min read How to Check Whether a Variable is Empty in PHP? Given some values of variables, the task is to check whether the variable is empty or not in PHP. An empty variable can refer to a variable that has not been set, a variable that has been explicitly set to an empty value, or a variable that contains a value that is considered empty. In this article, 5 min read PHP Sort array of strings in natural and standard orders You are given an array of strings. You have to sort the given array in standard way (case of alphabets matters) as well as natural way (alphabet case does not matter).Input : arr[] = {"Geeks", "for", "geeks"}Output : Standard sorting: Geeks for geeks Natural sorting: for Geeks geeks Input : arr[] = 2 min read ArrayObject uasort() Function in PHP The uasort() function of the ArrayObject class in PHP is used to sort values of an ArrayObject according to a user defined comparison function. The function compares and arranges the values present in the ArrayObject according to the given comparison function. This method does not affects the key-va 2 min read ArrayObject uksort() Function in PHP The uksort() function of the ArrayObject class in PHP is used to sort the entries present in the ArrayObject according to the keys following a user-defined function. They key-value mapping is preserved after sorting the ArrayObject. Syntax: void uksort($comparator) Parameters: This function accepts 2 min read Like