Home  >  Article  >  Backend Development  >  How to insert an array in PHP without affecting the original order_PHP Tutorial

How to insert an array in PHP without affecting the original order_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 09:59:401088browse

How to insert an array in php without affecting the original order

This article mainly introduces the method of inserting an array in php without affecting the original order. An example analysis of php The skills of array operation have certain reference value. Friends in need can refer to it

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

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

}

}

1 2

3

4 5

67 8 9 10 11 12
13
14
15 16 17 18 19 20 21 22 23
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); } }
I hope this article will be helpful to everyone’s PHP programming design. http://www.bkjia.com/PHPjc/975136.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/975136.htmlTechArticleHow to insert an array in php without affecting the original order. This article mainly introduces how to insert an array in php without affecting the original order. Methods to affect the original order, examples analyze the skills of PHP array operations, with...
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