Home >Backend Development >PHP Tutorial >PHP code to implement array insertion without affecting the original order

PHP code to implement array insertion without affecting the original order

不言
不言Original
2018-03-29 17:04:251175browse

This article shares with you a PHP code that implements inserting an array without affecting the original order. I hope it can help friends in need.

The example in this article describes how PHP implements inserting an array without affecting the original order. There is a sequential approach. Share it with everyone for your reference. The specific implementation method is as follows:

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);
 }
}

Related recommendations:

php implements the method of inserting an array without affecting the original order, php array_PHP tutorial

php implements a method of inserting an array without affecting the original order


The above is the detailed content of PHP code to implement array insertion without affecting the original order. For more information, please follow other related articles on the PHP Chinese website!

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