Home > Backend Development > PHP Tutorial > PHP sample code to determine whether an array is ordered

PHP sample code to determine whether an array is ordered

WBOY
Release: 2016-07-25 08:55:18
Original
1063 people have browsed it
  1. /**
  2. * Determine whether the array is in order
  3. * by bbs.it-home.org
  4. */
  5. function JudegSortArray($array) {
  6. if ($array [0] > $array [1]) {
  7. $flag = 1;
  8. } else {
  9. $flag = 0;
  10. }
  11. $temp = $flag;
  12. $len = count ( $array );
  13. for($i = 1; $i < $len; $i ++) {
  14. if ($flag == 0) {
  15. if ($array [$i] < $array [$i + 1])
  16. {
  17. continue;
  18. } else {
  19. $flag = 1;
  20. break;
  21. }
  22. }
  23. if ($flag == 1) {
  24. if ($array [$i] > $array [$i + 1]) {
  25. continue;
  26. } else
  27. {
  28. $flag = 0;
  29. break;
  30. }
  31. }
  32. }
  33. if ($flag != $temp) {
  34. echo "无序数组";
  35. } else {
  36. echo "有序数组";
  37. }
  38. }
  39. // 调用示例
  40. $array = array (
  41. 1,
  42. 2,
  43. 3,
  44. 4,
  45. 6,
  46. 5
  47. );
  48. $ret = JudegSortArray ( $array );
  49. echo $ret;
复制代码

>>> 您可能感兴趣的文章: php二维数组按指定键值key排序的例子 php根据键值对二维数组排序的小例子 php二维数组排序(实例) php数组排序的几个函数(附实例) php数组排序方法大全(程序员之家整理奉献) PHP数组排序方法总结(收藏) PHP各种排序算法的实现汇总 php 二维数组排序的两个例子 php 数组排序的实例代码 php数组实例之数组排序 php 数组排序实例分享(多种排序方式) php对二维数组进行排序 PHP二维数组排序自定义函数 php 二维数组按指定的键值排序的代码 php 数组排序(升序、降序及相关问题) php 简单数组排序的方法 学习php多维数组的排序 php多维数组排序的例子



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template