PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Thu Nov 14, 2019 12:49 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 1 post ] 
Author Message
PostPosted: Tue Aug 17, 2010 8:28 pm 
Offline
Forum Newbie

Joined: Mon Aug 02, 2010 3:50 pm
Posts: 9
I just created this `Rearranger` class which will move a value at a specified index to a location between a selected index and (selected index - 1). In other words, the value is inserted between two other values.

I don't think a function that solves the same problem is in the PHP language, please correct me if I am wrong.

Anyways, I would appreciate any feedback that you can provide.

Thanks!

Syntax: [ Download ] [ Hide ]
class Rearranger
{      
        /*
         * Determines how to move the specified value.
         */

        public static function go($list, $indexToMove, $offset)
        {
                if(($offset == ($indexToMove - 1)) || ($offset == ($indexToMove + 1))) {
                        //Value is only being moved up or down one index, so just swap the values
                        $list = self::swap($list, $indexToMove, $offset);
                } else if($offset < $indexToMove) {
                        //Value is being moved up (will be changing to a lower value index)
                        $list = self::move($list, $indexToMove, $offset);
                } else if($offset > $indexToMove) {
                        //Value will be moving down (will be changing to a higher value index)
                        $list = self::move($list, $indexToMove, $offset - 1);
                }
                return $list;
        }
       
        /*
         * Moves the value at $list[$indexToMove] in between
         * $list[$offset - 1] and $list[$offset].
         */

        public static function move($list, $indexToMove, $offset)
        {
                $container = $list[$indexToMove];
                unset($list[$indexToMove]);
                $list = array_values($list);
                $list2 = array_slice($list, $offset);
                $list = array_slice($list, 0, $offset);
                array_push($list, $container);
                return array_merge($list, $list2);
        }
       
        //Swap the values of two array indices
        public static function swap($list, $indexA, $indexB)
        {
                $vA = $list[$indexA];
                $vB = $list[$indexB];
                $list[$indexA] = $vB;
                $list[$indexB] = $vA;
                return $list;
        }
}
 


Here is how I use it:

Syntax: [ Download ] [ Hide ]
$a1 = array('a', 'b', 'c', 'd', 'e', 'f');

echo 'Before Rearranging:';
var_dump($a1);

echo  '4 to 1 (move up):';
$a1 = Rearranger::go($a1, 4, 1);
var_dump($a1);

echo '1 to 4 (move down):';
$a1 = Rearranger::go($a1, 1, 4);
var_dump($a1);

$a1 = Rearranger::go($a1, 2, 3);
echo '2 to 3 (swap):';
var_dump($a1);

echo '5 to 4 (swap):';
$a1 = Rearranger::go($a1, 5, 4);
var_dump($a1);

echo '3 to 3 (do nothing):';
$a1 = Rearranger::go($a1, 2, 2);
var_dump($a1);
 


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group