note 49579 deleted from ref.array by tularis

From: Date: Sun, 26 Aug 2007 16:39:06 +0000
Subject: note 49579 deleted from ref.array by tularis
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-129812@lists.php.net to get a copy of this message
Note Submitter: daniel _dot eherbert _at gmail _dot com ---- i was unable to use normal means to shift an array element up or down, so i had to write a function to do it... i havent had the chance to test it, but the method is there... <? $stuff[0] = 'a'; $stuff[1] = 'b'; $stuff[2] = 'c'; $stuff[3] = 'd'; function array_shift($array,$value,$direction = 'up') { $temp = $array; /* search for the value */ while ($array_value = current($array)) { if ($array_value == $value) { $key = key($array); break; } next($array); } /* no key */ if ( !isset($key) ) { return false; } /* check if we are at the top or bottom of the array and want to do an invalid move ie: top element up, bottom element down - return the original array... */ if ( $key == 0 && $direction == 'up' ) { return $array; } if ( $key == (count($array)-1) && $direction == 'down' ) { return $array; } /* reorder the elements */ switch ($direction) { case 'up': $factor = -1; break; case 'down': $factor = 1; break; } $temp[$key+$factor] = $array[$key]; $temp[$key] = $array[$key+$factor]; /* return the ordered array */ return $temp; } ?> $stuff = array_shift($stuff,'c','up'); - will move c up and b down $stuff = array_shift($stuff,'a','down'); - will move a down and b up $stuff = array_shift($stuff,'a','up'); - will return the original array $stuff = array_shift($stuff,'d','down'); - will return the original array ... if the element value could not be found it probably should return the original array instead of false...

« previous php.notes (#129812) next »