Home >Backend Development >PHP Tutorial >PHP determines whether an array is ordered_PHP tutorial
Today is the last day of the National Day holiday. I have no intention of working on business code. When I saw a discussion in the group about how PHP determines the order of an array, I implemented an algorithm with a complexity of N and posted the code for everyone to learn
[php]
function JudegSortArray($array) {
If ($array [0] > $array [1]) {
$flag = 1;
} else {
$flag = 0;
}
$temp = $flag;
$len = count ( $array );
for($i = 1; $i < $len; $i ++) {
If ($flag == 0) {
If ($array [$i] < $array [$i + 1])
Continue;
$flag = 1;
break;
}
If ($flag == 1) {
If ($array [$i] > $array [$i + 1]) {
Continue;
} else
$flag = 0;
break;
}
}
If ($flag != $temp) {
echo "Unordered array";
} else {
echo "Ordered array";
}
}
// Test case
$array = array (
1,
2,
3,
4,
6,
5
);
$ret = JudegSortArray ( $array );
echo $ret;
http://www.bkjia.com/PHPjc/477957.html
www.bkjia.com
http: //www.bkjia.com/PHPjc/477957.html