0

I have an array of values, something like;

$array = array(1,2,3,4); 

I want to be able to reposition an element and reorder.
Edit: To be clear on this point I don't just want to swop elements around, I want to move an element to a new place in the array and maintain the order of the other elements. For example;

// move value 3 to index[1], result
$array(1,3,2,4);
// or move value 1 to index[3], result
$array[2,3,4,1);

To make it clearer if required;

$array('alice','bob','colin','dave');
// move value 'colin' to index[1], result
$array('alice','colin','bob','dave');
// or move value 'alice' to index[3], result
$array('bob','colin','dave', 'alice');

Any ideas please.

3
  • Just to clarify: You want to take a value and move it to a specific index? Commented Feb 28, 2014 at 14:20
  • @JosefOttosson, yes. I would like to select what to move by its value if possible. Commented Feb 28, 2014 at 14:34
  • Maybe you could make this clearer by changing your example to include letters instead of numbers. All the replies so far take to indexes as arguments. Not that it's difficult to find the index of a value, but most answers miss that part of the question. Do you care about the value of the index? Are you interested only in numerical indexes? Commented Feb 28, 2014 at 14:43

2 Answers 2

1

This is copied from another StackOverflow thread by user hakre, but this function should work:

$array = array(1,2,3,4);
function moveElement(&$array, $a, $b) {
    $out = array_splice($array, $a, 1);
    array_splice($array, $b, 0, $out);
}

moveElement($array, 3, 1); // would move the value of the element at position [3] (the number 4 in the array example) to position [1]
//would output: Array ( [0] => 1 [1] => 4 [2] => 2 [3] => 3 )

It takes in the $array array and repositions element 3 to the position of [1] in the example. Use the function arguments to move whatever element value (in the example 3) to whatever position (in the example 1) you desire.

Sign up to request clarification or add additional context in comments.

6 Comments

Yes. Care to elaborate on your comment any?
This will result in 1, 4, 2, 3.
Thanks. I edited my post to reflect a '4' instead of the '3' I originally wrote by accident.
Yes, but OP wants 2,3,1,4
Link to original source?
|
1

Try this code:

function swap_value(&$array,$first_index,$last_index){
    $save=$array[$first_index];
    $array[$first_index]=$array[$last_index];
    $array[$last_index]=$save;
    return $array;
}
$array = array(1,2,3,4); 
var_dump(swap_value($array,1,2));
var_dump(swap_value($array,0,2));

2 Comments

Will fail on associative arrays at least (because swapping values, not keys). Also keys would be untouched (so values will be swapped while keys will be not)
@AlmaDo Ok.Changed function name to swap_value :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.