note 55716 deleted from function.end by danbrown
| From: | danbrown@php.net | Date: | Wed, 15 Jun 2011 23:03:29 +0000 |
| Subject: | note 55716 deleted from function.end by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-180584@lists.php.net to get a copy of this message | ||
Note Submitter: mika dot stenberg at helsinki dot fi
----
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);
}