How to Move an Array Element with a Known Key to the End of an Array in PHP

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;

Shift an element to the end of array

If the index is unknown:

foreach($array as $key => $val) {
if($val->pagerank == 'R') {
$item = $array[$key];
unset($array[$key]);
array_push($array, $item);
break;
}
}

If you don't want to modify the array while it is being iterated over just find the index then make the modifications.

$foundIndex = false;
foreach($array as $key => $val) {
if($val->pagerank == 'R') {
$foundIndex = $key;
break;
}
}
if($foundIndex !== false) {
$item = $array[$foundIndex];
unset($array[$foundIndex]);
array_push($array, $item);
}

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 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

How do I move an array element with a known key weight of an array in PHP?

Here is the function if it can help some people

function reorder_array(&$my_array) {   
foreach ($my_array as $key => $object) {
$move = $object['weight'];
$arr = $my_array;
if ($move == 0 || !isset($arr[$key])) {
continue;
}
$i = 0;
foreach($arr as &$val){
$val = array('sort' => (++$i * 10), 'val' => $val);
}
$arr[$key]['sort'] = $arr[$key]['sort'] + ($move * 10 + ($key == $key ? ($move < 0 ? -5 : 5) : 0));

uasort($arr, function($a, $b) {
return $a['sort'] > $b['sort'];
});
foreach($arr as &$val) {
$val = $val['val'];
}
$my_array = $arr;
}
}

Source of my solution

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',
];

Associative array move last element to first

simplest way to do with below code

$arr = array(
'key1'=>'value1',
'key2'=>'value2',
'key3'=>'value3'
);

$lastvalue = end($arr);
$lastkey = key($arr);

$arr1 = array($lastkey=>$lastvalue);

array_pop($arr);

$arr1 = array_merge($arr1,$arr);

OUTPUT

Array
(
[key3] => value3
[key1] => value1
[key2] => value2
)

Moving array elements to end of array by sorting

Here's one way to do it with uasort:

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

uasort($arr, function($a, $b){
if ($a == 4) return -1;
if ($b == 4) return 1;
return 0;
});

$arr = array_reverse($arr, true);

print_r($arr);

The true argument to array_reverse preserves the keys in the array. The return 0 is optional but improves clarity in my opinion.

Output:

Array
(
[0] => 1
[3] => 2
[1] => 2
[4] => 3
[5] => 5
[7] => 2
[8] => 1
[2] => 4
[6] => 4
)

The fact that some of the keys corresponding to the same value are mixed up is unavoidable.

Php array move element to other postion with key value pair

Live on ide1: http://ideone.com/yJ1e3N

Use uasort to keep key-value association while ordering with a custom logic using a closure:

$order = [
'Birthday' => 1,
'Congratulations' => 2,
'Halloween' => 3,
'Christmas' => 4,
'Mothers Day' => 5
];

uasort($array, function($a,$b) use ($order){
return $order[$a] > $order[$b];
});

With this script you can use any custom order logic you need by assigning the right value to the array $order.
This will be also very fast if you have many elements as the right order is accessed using the keys of the $order array (and not by a linear scan).



Related Topics



Leave a reply



Submit