note 55716 added to function.end
| From: | mika dot stenberg at helsinki dot fi | Date: | Thu, 11 Aug 2005 13:24:19 +0000 |
| Subject: | note 55716 added to function.end | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-93438@lists.php.net to get a copy of this message | ||
Here's something useful with arrays that I did: change an items position up or down in array.
Thanks to previous posters for some useful functions (included):
// $mode -1 (negat) moves the item down, +1 (pos) moves the item up
// I used this because i parsed the mode from the query string
// You get the picture though
// $from_id points which item to move
function change_pos($array, $from_id, $mode){
// get hold of the beginning and the end
$start = first($array);
$end = last($array);
reset($array);
// find the position in an array
array_set_current($array, $from_id);
// remember the old value
$temp = $archive[ $from_id ];
// move down
if ($uusi_id < 0){
if (key($array) != $end) {
$array[ $from_id ] = next($array);
$array[key($aray)] = $temp;
}
else echo "Couldnt move it!";
}
// move up
else if ( key($array) != $start ) {
$array[ $from_id ] = prev($array);
$array[key($array)] = $temp;
save($array);
}
else echo "Couldnt do it !";
}
function array_set_current(&$array, $key){
reset($array);
while(current($array)){
if(key($array) == $key){
break;
}
next($array);
}
}
function first(&$array) {
if (!is_array($array)) return null;
if (!count($array)) return null;
reset($array);
return key($array);
}
function last(&$array) {
if (!is_array($array)) return null;
if (!count($array)) return null;
end($array);
return key($array);
}
----
Manual Page -- http://www.php.net/manual/en/function.end.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+55716
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+55716&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+55716&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+55716&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+55716&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+55716&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+55716&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+55716&report=yes
Search -- http://master.php.net/manage/user-notes.php