Move an Array Element to a New Index in PHP

Move an array element to a new index in PHP

As commented, 2x array_splice, there even is no need to renumber:

$array = [
0 => 'a',
1 => 'c',
2 => 'd',
3 => 'b',
4 => 'e',
];

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

moveElement($array, 3, 1);

redzarf comments: "To clarify $a is $fromIndex and $b is $toIndex"

Result:

[
0 => 'a',
1 => 'b',
2 => 'c',
3 => 'd',
4 => 'e',
];

Move array item with certain key to the first position in an array, PHP

No need to unset keys. To keep it short just do as follow

//appending $new in our array 
array_unshift($arr, $new);
//now make it unique.
$final = array_unique($arr);

Demo

Move an element of an array from start to end

Within your id_fine function reset the array values of your array using array_values

$array_disordinato[0] = array_values($array_disordinato[0]);
return $array_disordinato

Move an array element from one array position to another

If you'd like a version on npm, array-move is the closest to this answer, although it's not the same implementation. See its usage section for more details. The previous version of this answer (that modified Array.prototype.move) can be found on npm at array.prototype.move.


I had fairly good success with this function:

function array_move(arr, old_index, new_index) {    if (new_index >= arr.length) {        var k = new_index - arr.length + 1;        while (k--) {            arr.push(undefined);        }    }    arr.splice(new_index, 0, arr.splice(old_index, 1)[0]);    return arr; // for testing};
// returns [2, 1, 3]console.log(array_move([1, 2, 3], 0, 1));

Moving up/down an item in the array by its value

Shifting up (assuming you've checked that the item is not already the first one):

$item = $array[ $index ];
$array[ $index ] = $array[ $index - 1 ];
$array[ $index - 1 ] = $item;

Shifting down:

$item = $array[ $index ];
$array[ $index ] = $array[ $index + 1 ];
$array[ $index + 1 ] = $item;

How do I move an array element with a known key to the end of an array in PHP?

The only way I can think to do this is to remove it then add it:

$v = $my_array['monkey'];
unset($my_array['monkey']);
$my_array['monkey'] = $v;

How to rearrange item of an array to new position in Swift?

Swift 3.0+:

let element = arr.remove(at: 3)
arr.insert(element, at: 2)

and in function form:

func rearrange<T>(array: Array<T>, fromIndex: Int, toIndex: Int) -> Array<T>{
var arr = array
let element = arr.remove(at: fromIndex)
arr.insert(element, at: toIndex)

return arr
}

Swift 2.0:

This puts 3 into position 4.

let element = arr.removeAtIndex(3)
arr.insert(element, atIndex: 2)

You can even make a general function:

func rearrange<T>(array: Array<T>, fromIndex: Int, toIndex: Int) -> Array<T>{
var arr = array
let element = arr.removeAtIndex(fromIndex)
arr.insert(element, atIndex: toIndex)

return arr
}

The var arr is needed here, because you can't mutate the input parameter without specifying it to be in-out. In our case however we get a pure functions with no side effects, which is a lot easier to reason with, in my opinion.
You could then call it like this:

let arr = [1,2,3,4]
rearrange(arr, fromIndex: 2, toIndex: 0) //[3,1,2,4]


Related Topics



Leave a reply



Submit