note 115025 added to function.array-shift
| From: | michaeljanikk at gmail dot com | Date: | Wed, 14 May 2014 01:26:56 +0000 |
| Subject: | note 115025 added to function.array-shift | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-199534@lists.php.net to get a copy of this message | ||
To remove an element from the MIDDLE of an array (similar to array_shift, only instead of removing
the first element, we want to remove an element in the middle, and shift all keys that follow down
one position)
Note that this only works on enumerated arrays.
<?php
$array = array('a', 'b', 'c', 'd', 'e',
'e');
/*
array(6) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
[2]=>
string(1) "c"
[3]=>
string(1) "d"
[4]=>
string(1) "e"
[5]=>
string(1) "e"
}
*/
$indexToRemove = 2;
unset($array[$indexToRemove]);
$array = array_slice($array, 0);
/*
array(5) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
[2]=>
string(1) "d"
[3]=>
string(1) "e"
[4]=>
string(1) "e"
}
*/
?>
I hope this helps someone!
----
Server IP: 72.52.91.14
Probable Submitter: 99.177.64.34
----
Manual Page -- http://php.net/manual/en/function.array-shift.php
Edit -- https://master.php.net/note/edit/115025
Del: integrated -- https://master.php.net/note/delete/115025/integrated
Del: useless -- https://master.php.net/note/delete/115025/useless
Del: bad code -- https://master.php.net/note/delete/115025/bad+code
Del: spam -- https://master.php.net/note/delete/115025/spam
Del: non-english -- https://master.php.net/note/delete/115025/non-english
Del: in docs -- https://master.php.net/note/delete/115025/in+docs
Del: other reasons-- https://master.php.net/note/delete/115025
Reject -- https://master.php.net/note/reject/115025
Search -- https://master.php.net/manage/user-notes.php