15
$arr = array(
    'a1'=>'1',
    'a2'=>'2'
);

I need to move the a2 to the top, as well as keep the a2 as a key how would I go on about it I can't seem to think a way without messing something up :)

3
  • What exactly are you trying to do? Is it sort the array in reverse numerical order? Commented Mar 15, 2011 at 14:04
  • Have you managed to solve the problem? Commented Mar 15, 2011 at 14:19
  • yes but u need to wait a few minutes before you can mark as accepted thnx m8 Commented Mar 15, 2011 at 14:22

5 Answers 5

63

Here is a solution which works correctly both with numeric and string keys:

function move_to_top(&$array, $key) {
    $temp = array($key => $array[$key]);
    unset($array[$key]);
    $array = $temp + $array;
}

It works because arrays in PHP are ordered maps.
Btw, to move an item to bottom use:

function move_to_bottom(&$array, $key) {
    $value = $array[$key];
    unset($array[$key]);
    $array[$key] = $value;
}
Sign up to request clarification or add additional context in comments.

1 Comment

A better answer, since it doesn't assume that the key to be moved is at the end.
14

You can achieve that this way:

$arr = array(
    'a1'=>'1',
    'a2'=>'2'
);

end($arr);

$last_key     = key($arr);
$last_value   = array_pop($arr);
$arr          = array_merge(array($last_key => $last_value), $arr);

/*
    print_r($arr);

    will output (this is tested):
    Array ( [a2] => 2 [a1] => 1 )
*/

5 Comments

This is not fully correct: if the arrays contain numeric keys, the keys will be modified.
Still not fully correct: array_merge will renumber numeric keys. Probably useful enough, though.
@awm: The key values are not numeric. My print_r() output is Array ( [a2] => 2 [a1] => 1 ).
Exactly. That's why this solution is probably useful enough to get the job done. Jon's first comment is still true, though.
This doesn't scale. What if you have an array with 5 keys and you want to move the 3rd key to the top?
5

try this:

$key = 'a3';
$arr = [
    'a1' => '1',
    'a2' => '2',
    'a3' => '3',
    'a4' => '4',
    'a5' => '5',
    'a6' => '6'
];
if (isset($arr[ $key ]))
    $arr = [ $key => $arr[ $key ] ] + $arr;

result:

array(
    'a3' => '3',
    'a1' => '1',
    'a2' => '2',
    'a4' => '4',
    'a5' => '5',
    'a6' => '6'
)

1 Comment

Ahhh, simple, elegant, perfect!
1

Here's a simple one liner to get this done with array_splice() and the union operator:

$arr = array('a1'=>'1', 'a2'=>'2', 'a3' => '3');
$arr = array_splice($arr,array_search('a2',array_keys($arr)),1) + $arr;

Edit:

In retrospect I'm not sure why I wouldn't just do this:

$arr = array('a2' => $arr['a2']) + $arr;

Cleaner, easier and probably faster.

Comments

-1

You can also look at array_multisort This lets you use one array to sort another. This could, for example, allow you to externalize a hard-coded ordering of values into a config file.

<?php
   $ar1 = array(10, 100, 100, 0);
   $ar2 = array(1, 3, 2, 4);
   array_multisort($ar1, $ar2);

   var_dump($ar1);
   var_dump($ar2);
?>

In this example, after sorting, the first array will contain 0, 10, 100, 100. The second array will contain 4, 1, 2, 3. The entries in the second array corresponding to the identical entries in the first array (100 and 100) were sorted as well.

Outputs:

array(4) {
  [0]=> int(0)
  [1]=> int(10)
  [2]=> int(100)
  [3]=> int(100)
}
array(4) {
  [0]=> int(4)
  [1]=> int(1)
  [2]=> int(2)
  [3]=> int(3)
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.