Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1000099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:27:28+00:00 2026-05-16T07:27:28+00:00

Is there a PHP function that will do the following: Move a specified array

  • 0

Is there a PHP function that will do the following:

Move a specified array value from it’s current index and then either:

  1. Moved in between two existing indices.
  2. Swapped with a second index/value.

I made the class below for the task, but wonder if there is a pre-existing function in the PHP library?

Here is the class I have created for the task:

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 are some example usages of the Rearranger class:

$a1 = array('a', 'b', 'c', 'd', 'e', 'f');

function display($list) { echo '<p>' . var_dump($list) . '</p>'; }

//Move value at index 4 to between index 0 and 1
$a1 = Rearranger::go($a1, 4, 1);
display($a1);

//Move value at index 1 to between index 3 and 4
$a1 = Rearranger::go($a1, 1, 4);
display($a1);

//Swap value 2 and 3    
$a1 = Rearranger::go($a1, 2, 3);
display($a1);

//Swap value 5 and 4 
$a1 = Rearranger::go($a1, 5, 4);
display($a1);

//Do nothing
$a1 = Rearranger::go($a1, 2, 2);
display($a1);
  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T07:27:28+00:00Added an answer on May 16, 2026 at 7:27 am

    What you mean is a sort algorithm right? Well there are loads of functions to sort arrays in php. Wich function you need depends on the exact requirements by witch you want to sort.

    Alot of sort alghoritms have been devised over the years, take a look.

    EDIT
    How about:

    $source = array('a','b','c','d');
    $head = array_splice($source,0,2);
    $tail = array_splice($source,2,4);
    $new = array_merge($head,array('e'),$tail);
    // should yield array('a','b','e','c','d')
    // Please check the array_splice documentation, iam not a 100% sure on the syntax.
    

    This way you should be able to insert a new element into the array. Should help you on your way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following php function, that creates an array of all the days between
Is there a php function that someone can use to automatically detect if an
Is there a PHP function that grabs the string in between two characters. For
Is there a PHP function that takes an integer and return the Unicode character
Is there a PHP string function that transforms a multi-line string into a single-line
There seems to be a bug in a Wordpress PHP function that leaves whitespace
Is there any equivalent function that returns the character at position X in PHP?
is there a function in PHP that could parse the html data below to
I am wondering if there a function in php that can allow me put
Is there any way that I could enjoy a decodeValue() function in PHP, too?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.