Home  >  Article  >  Backend Development  >  php实现插入数组但不影响原有顺序的方法_php技巧

php实现插入数组但不影响原有顺序的方法_php技巧

WBOY
WBOYOriginal
2016-05-16 20:19:04829browse

本文实例讲述了php实现插入数组但不影响原有顺序的方法。分享给大家供大家参考。具体实现方法如下:

function array_intsort($array,$num) {
 $array_right = $array_left = array();
 $length = count($array);
 if ($num < $array[0]) {
  array_unshift($array,$num);
  return $array;
 } else {
  for($i=0; $i < $length; $i++) {
   if ($i + 1 < $length) {
    if ($array[$i] < $num && $num < $array[$i+1]) {
     $array_right = array_slice($array,$i + 1);    
     $array_left = array_slice($array,0,$i);
     $array_left[] = $num;
     break;
    }
   } else {
    $array_left = $array;
    $array_left[] = $num;
   }
  }
  return array_merge($array_left,$array_right);
 }
}

希望本文所述对大家的php程序设计有所帮助。

Statement:
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