Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

In PHP, an array is considered unordered by default. However, you can manually sort an array using functions such as sort(), ksort(), asort(), usort(), uasort(), and uksort().

To check if an array is already sorted, you can use the function called array_values(). This function returns all the values of an array in a numerical indexed array. You can then compare the original array with the indexed array to check if they are the same.

Here's an example:

// unordered array
$fruits = array("banana", "apple", "orange", "lemon");

// sorted array
sort($fruits);

// check if array is sorted
if ($fruits === array_values($fruits)) {
    echo 'Array is sorted';
} else {
    echo 'Array is not sorted';
}