PHP 8.3.21 Released!

Voting

: nine minus eight?
(Example: nine)

The Note You're Voting On

gilberg_vrn
9 years ago
array_splice with preserve keys

<?php
function array_splice_preserve_keys(&$array, $from, $length = null) {
$result = array_slice($array, $from, $length, true);
$array = array_slice($array, $from + $length, null, true);

return
$result;
}
?>

Example:

<?php
$array
= [
1 => 'a',
2 => 'b',
26 => 'z'
];

var_dump(array_splice_preserve_keys($array, 0, 1), $array);
/**
* array(1) {
* [1]=>
* string(1) "a"
* }
* array(2) {
* [2]=>
* string(1) "b"
* [26]=>
* string(1) "z"
* }
*/
?>

<< Back to user notes page

To Top