-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_splice.php
More file actions
39 lines (36 loc) · 1022 Bytes
/
Copy patharray_splice.php
File metadata and controls
39 lines (36 loc) · 1022 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php
function splice( &$input, $offset, $length = 0, $replacement = NULL ) {
$i = 0;
$input = (array) $input;
$count = count($input);
end($input);
$lastKey = key($input);
$result = array();
$delete = array();
$offset = $offset < 0 ? (count($input)) + $offset : $offset;
if($offset>$count){
$offset = $count;
}
$length = $length < 0 ? (count($input) + $length) - $offset : $length;
if($length < 0)
$length = 0;
if($length + $offset > $count) {
$length = $count - $offset;
}
$flag = false;
reset($input);
for($i=0; $i<$offset; $i++,next($input)){
$result[] = current($input);
}
for($i=$length;$i>0;$i--,next($input)){
$delete[] = current($input);
}
foreach($replacement as $value){
$result[] = $value;
}
for($c = count($input),$i=$offset+$length;$i<$c;$i++,next($input)){
$result[] = current($input);
}
$input = $result;
return $delete;
}