note 92651 added to function.array-splice
| From: | danielecentamore at osu1 dot php dot net | Date: | Sun, 02 Aug 2009 14:48:16 +0000 |
| Subject: | note 92651 added to function.array-splice | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-159003@lists.php.net to get a copy of this message | ||
just useful functions to move an element using array_splice.
<?php
// info at danielecentamore dot com
// $input (Array) - the array containing the element
// $index (int) - the index of the element you need to move
function moveUp($input,$index) {
$new_array = $input;
if((count($new_array)>$index) && ($index>0)){
array_splice($new_array, $index-1, 0, $input[$index]);
array_splice($new_array, $index+1, 1);
}
return $new_array;
}
function moveDown($input,$index) {
$new_array = $input;
if(count($new_array)>$index) {
array_splice($new_array, $index+2, 0, $input[$index]);
array_splice($new_array, $index, 1);
}
return $new_array;
}
$input = array("red", "green", "blue", "yellow");
$newinput = moveUp($input, 2);
// $newinput is array("red", "blue", "green", "yellow")
$input = moveDown($newinput, 1);
// $input is array("red", "green", "blue", "yellow")
?>
----
Server IP: 213.92.11.19
Probable Submitter: 151.30.36.26
----
Manual Page -- http://www.php.net/manual/en/function.array-splice.php
Edit -- https://master.php.net/note/edit/92651
Del: integrated -- https://master.php.net/note/delete/92651/integrated
Del: useless -- https://master.php.net/note/delete/92651/useless
Del: bad code -- https://master.php.net/note/delete/92651/bad+code
Del: spam -- https://master.php.net/note/delete/92651/spam
Del: non-english -- https://master.php.net/note/delete/92651/non-english
Del: in docs -- https://master.php.net/note/delete/92651/in+docs
Del: other reasons-- https://master.php.net/note/delete/92651
Reject -- https://master.php.net/note/reject/92651
Search -- https://master.php.net/manage/user-notes.php