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);
}