Moving array element to top in PHP

All we need is an easy explanation of the problem, so here it is.

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

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

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

Method 2

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 )
*/

Method 3

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

Method 4

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.

Method 5

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

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply