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 8662613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:46:32+00:00 2026-06-12T16:46:32+00:00

Swap-up element in a multidimensional array with the sibling above it. I want the

  • 0

Swap-up element in a multidimensional array with the sibling above it.

I want the element with the selected index in the array to swap it’s position with the one above him.

  1. The element from it’s position(N) to go to position(N-1)
  2. I want the element at position(N-1) to go at position(N),
  3. The resulting index should be reflecting correctly their new order in the array. array_values($tmparr); does sort the index correctly
  4. The Target Element to swap up can go to Position(0) but will never start at Position(0)
  5. The Element to swap down if at Position(0) Should go at position(1) not go at the End of the array.

Although this function explain semantically what i want to do it does not work at all.

function swaparray($tmparr,$posa,$posb){
 $vala = $tmparr[$posa];
 $valb = $tmparr[$posb];
 $tmparr[$posa] = $valb;
 $tmparr[$posb] = $vala;
 return $tmparr; }

The 2nd function shifts the intended target up but the above element is pushed up and goes to the end of the list if he is at position 0, it does not go under the target, so it doesnt work as intended it

 function swaparray($tmparr,$posa,$posb){
  $vala = $tmparr[$posa];
  $valb = $tmparr[$posb];
  unset($tmparr[$posa]);
  unset($tmparr[$posb]);
  $tmparr[$posa] = $valb;
  $tmparr[$posb] = $vala;
  $tmparr = array_values($tmparr);
 return $tmparr;
}

Reading further about my issue is seams Array_splice() could do the trick. What are your inputs about this?

Edit Answer: (PHP >= 4.3.8 )

The working solution with Array_splice()

 function swaparray($array, $n) {
     // taking out at $n
     $out = array_splice($array, $n, 1);
     // adding in at $n - 1
    array_splice($array, $n - 1, 0, $out);
    return $array;
 }

Here is the original multidimensional array

 Array ( [0] => Array ( [key1] => 1 [key2] => 1 [key3] => 1 [key4] => 1 )
         [1] => Array ( [key1] => 2 [key2] => 2 [key3] => 2 [key4] => 2 ) 
         [2] => Array ( [key1] => 3 [key2] => 3 [key3] => 3 [key4] => 3 ) 
         [3] => Array ( [key1] => 4 [key2] => 4 [key3] => 4 [key4] => 4 ) )

Here is an excerpt / exemple of want i wanted it to do.

[0] key1=1 key2=1 key3=1 key4=1
[1] key1=2 key2=2 key3=2 key4=2 
[2] key1=3 key2=3 key3=3 key4=3 <- 
[3] key1=4 key2=4 key3=4 key4=4

swaparray($tmparr,2);

[0] key1=1 key2=1 key3=1 key4=1
[1] key1=3 key2=3 key3=3 key4=3 <- 
[2] key1=2 key2=2 key3=2 key4=2 
[3] key1=4 key2=4 key3=4 key4=4     

swaparray($tmparr,1);

[0] key1=3 key2=3 key3=3 key4=3 <- 
[1] key1=1 key2=1 key3=1 key4=1
[2] key1=2 key2=2 key3=2 key4=2 
[3] key1=4 key2=4 key3=4 key4=4 

swaparray($tmparr,1);

[0] key1=1 key2=1 key3=1 key4=1 <-
[1] key1=3 key2=3 key3=3 key4=3  
[2] key1=2 key2=2 key3=2 key4=2 
[3] key1=4 key2=4 key3=4 key4=4 
  • 1 1 Answer
  • 0 Views
  • 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-06-12T16:46:34+00:00Added an answer on June 12, 2026 at 4:46 pm

    The element from it’s position(N) to go to position(N-1)
    I want the element at position(N-1) to go at position(N),

    All you say is that you want to swap the two, where N is never 0 in a zero indexed array.

    Move Element N to N-1:

    /** 
      * [0] is top
      */
    function moveUp(&$array, $n) {
        if ($n < 1)             throw new InvalidArgumentException();
        if (!isset($array[$n])) throw new InvalidArgumentException();
        // taking out at $n
        $out = array_splice($array, $n, 1);
        // adding in at $n - 1
        array_splice($array, $n - 1, 0, $out);
    }
    

    Usage:

    $n = 2;
    moveUp($array, $n);
    var_dump($array);
    

    Because the element at N-1 will get one element added in front, it will automatically move to N. Job done. array_splice is really powerful.

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

Sidebar

Related Questions

I want to swap out one view with another by pushing the old view
Is it possible to swap a reference of an object that an array element
An array a[] contains all of the integers from 0 to N, except one.
How exactly does Javascript's array.reverse() work? Does it go through and swap every element
How to move element in array in order or remove from array? Example: arr[3,3,2,1]
I want to swap both references, is there a nice way make such a
code should do this: a) Given an unsorted array of integers, your task is
I'm trying to use jQuery to swap the background image of an element in
I'd like to know how to swap every second element of a list in
How do I say, slide an element a few pixels to the side from

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.