24

What's the most elegant way in PHP to move an array element chosen by key to the first position?

Input:

$arr[0]=0;
$arr[1]=1;
$arr[2]=2;
....
$arr[n]=n;
$key=10;

Output:

$arr[0]=10;
$arr[1]=0;
$arr[2]=1;
$arr[3]=2;
....
$arr[n]=n;
tshepang
  • 12,111
  • 21
  • 91
  • 136
user965748
  • 2,227
  • 4
  • 22
  • 30
  • 1
    In the example, you're not "choosing element by key". You're just inserting an arbitrary value to the beginning of the array. I think this might confuse other people – galymzhan Jul 28 '12 at 19:00
  • 2
    @galymzhan I think it's implied that $arr[10] was previously 10, and now it goes from $arr[9]=9 to $arr[10]=11. I agree that the example doesn't illustrate this clearly. It would be better to a) show the array element that's being "moved", and b) use different element values so it's harder to confuse a key with its value. – octern Jul 28 '12 at 19:02
  • It seems the best will be the good old way with a temporary array and cycle – user965748 Jul 28 '12 at 19:04
  • @octern Yes, I think you're right, because size of `$arr` remains the same `$arr[n] = n` – galymzhan Jul 28 '12 at 19:05

9 Answers9

40

Use array_unshift:

$new_value = $arr[n];
unset($arr[n]);
array_unshift($arr, $new_value);
ryanyuyu
  • 6,366
  • 10
  • 48
  • 53
Yan Berk
  • 14,328
  • 9
  • 55
  • 52
  • The same array, it looks as if it doesn't change keys – user965748 Jul 28 '12 at 19:01
  • 1
    oops edit: I was printing out keys, let me try once more. Of course it works. I'm sorry for the confusion – user965748 Jul 28 '12 at 19:06
  • Glad it helped. Notice that the array needs to be reindexed afterwards. Read here how to do this: http://stackoverflow.com/questions/591094/how-do-you-reindex-an-array-in-php – Yan Berk Jul 28 '12 at 19:11
15

Old question, and already answered, but if you have an associative array you can use array_merge.

$arr = array_merge([$key=>$arr[$key]], $arr);

EDITED (above to show PHP7+ notation, below is example)

$arr = ["a"=>"a", "b"=>"b", "c"=>"c", "d"=>"d"];
$arr = array_merge(["c"=>$arr["c"]], $arr);

The effective outcome of this operation

$arr == ["c"=>"c", "a"=>"a", "b"=>"b", "d"=>"d"]
Justin
  • 541
  • 7
  • 15
  • 1
    Doesn't this code **copy** the element instead of **moving** it? – Gonzalingui Jun 19 '18 at 18:07
  • @Gonzalingui, You may be right! Doesn't it depends on the value in the array, though? If it is scalar, it might copy it but if it is a reference value then wouldn't it only copy the reference and not the value? (I could be wrong, but I don't think the OP was asking more about changing the value in an array variable than the underlying computer science fundamentals of moving versus copying a value in held in memory. :) ) – Justin Jun 22 '18 at 13:04
13

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

wp student
  • 755
  • 9
  • 24
  • 7
    This solution works only for arrays containing numbers and ignoring it's key. Also, it's inserting a new element, but the question asks how to move an existent array item to the first position. It clearly doesn't answer the question. – Gonzalingui Jun 19 '18 at 18:04
7

Something like this should work. Check if the array key exists, get its value, then unset it, then use array_unshift to create the item again and place it at the beginning.

if(in_array($key, $arr)) {
    $value = $arr[$key];
    unset($arr[$key]);
    array_unshift($arr, $value);
}
leejmurphy
  • 994
  • 3
  • 17
  • 28
4
<?php
$key = 10;
$arr = array(0,1,2,3);
array_unshift($arr,$key);
var_dump($arr) //10,0,1,2,3
?>
Opi
  • 1,288
  • 1
  • 10
  • 14
  • 1
    This prepends the chosen element, but it doesn't reference it by key and remove it from elsewhere in the array. – octern Jul 28 '12 at 18:59
4

Since any numeric key would be re-indexed with array_unshift (as noted in the doc), it's better to use the + array union operator to move an item with a certain key at the first position of an array:

$item = $arr[$key];
unset($arr[$key]);
$arr = array($key => $item) + $arr;
Veve
  • 6,643
  • 5
  • 39
  • 58
3
$arr[0]=0;
$arr[1]=1;
$arr[2]=2;
$arr[3]=10;


$tgt = 10;
$key = array_search($tgt, $arr);
unset($arr[$key]);
array_unshift($arr, $tgt);

// var_dump( $arr );
array
0 => int 10
1 => int 0
2 => int 1
3 => int 2
Cups
  • 6,901
  • 3
  • 26
  • 30
0
$tgt = 10;
$key = array_search($tgt, $arr);
for($i=0;$i<$key;$i++)
{
   $temp = $arr[$i];
   $arr[$i] = $tgt;
   $tgt = $temp;
}

After this simple code, all you need to do is display the re-arranged array. :)

0

For example, so: $arr = [$key => $arr[$key]] + $arr;

Maxim Mandrik
  • 377
  • 1
  • 5
  • 11