8

I have an array

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

I want create it like

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

I just want to move the last element to first with key value pair keeping all other details as it is.

nevermourn
  • 684
  • 1
  • 6
  • 17
Satyendra Mishra
  • 523
  • 2
  • 9
  • 21
  • 5
    Neither you nor your code should know or care about what order an associative array's keys are in - often it is unspecified, and it doesn't matter anyway. If you care about order, use a numeric array. – michaelb958--GoFundMonica May 03 '13 at 12:03
  • No its not a numeric array. – Satyendra Mishra May 04 '13 at 15:03
  • It is an associative array. Actual this is not my real array. – Satyendra Mishra May 04 '13 at 15:04
  • The real array is very big so I just entered a sample. Anyways I got my answer from the below mentioned post. – Satyendra Mishra May 04 '13 at 15:06
  • `$firstAsLast = array_merge(array_splice($array, 1), $array);` or `$lastAsFirst = array_merge(array_splice($array,-1), $array);` – yckart Apr 30 '15 at 14:10
  • 1
    This should be reopened IMO, but I lack the rep to do so. The close reason provided is absurd - this is extremely clear and answerable. This *does* matter in some cases: consider an array of 50 states (`'AK' => 'Alaska') that is unsorted. Outputting it with a `foreach` would output it in the unsorted fashion. Sorting/determining the order of associative arrays is **encouraged** by the PHP manual by it's implementation of [`ksort()`](http://php.net/manual/en/function.ksort.php) which *specifically calls out being useful for assoc arrays*. – HPierce Nov 25 '15 at 19:09
  • I tried finding a duplicate, but could not find one either. [This question](http://stackoverflow.com/questions/12696330/associative-array-move-an-element-to-first-position) has a very relevant title, but is premised on the fact that elements are added to the array to be sorted - thus, justifying it's marked duplicate. This one does not use that premise and might be useful if an array is provided by a 3rd party library. – HPierce Nov 25 '15 at 19:14

5 Answers5

15

Just a matter of splicing it and merging it to the beginning

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

$newArray = array_merge(array_splice($array, -1), $array);

var_dump($newArray);

Output:

array(3) {
  ["key3"]=>
  string(6) "value3"
  ["key1"]=>
  string(6) "value1"
  ["key2"]=>
  string(6) "value2"
}
nvanesch
  • 2,540
  • 14
  • 25
13

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
)
Dharman
  • 30,962
  • 25
  • 85
  • 135
liyakat
  • 11,825
  • 2
  • 40
  • 46
0

User the function array_reverse() to reverse the array, Like :

<?php
$arr=array(
   'key1'=>'value1',
   'key2'=>'value2',
   'key3'=>'value3'
);
$reversed = array_reverse($arr);
echo "<pre>";
print_r($reversed);
?>
Vinod VT
  • 6,946
  • 11
  • 51
  • 75
0
$array = array(
    'key1' => 'value1',
    'key2' => 'value2',
    'key3' => 'value3'
);

$count = 1;
foreach ($array as $k => $v) {
    if ($count == 1)
        $first[$k] = $v;
    if ($count == count($array))
        $last[$k] = $v;

    $count++;
}

array_shift($array);
array_pop($array);
$final = array_merge($last, $array, $first);
print_r($final);
Nanhe Kumar
  • 15,498
  • 5
  • 79
  • 71
0

first goto last item or array, use end(fastest one) for that purpose.Now fetch key,value then delete it and add to top.

$last_val = end($original_array);
$last_key = key($original_array);
unset($original_array[$key]);
$new_array = array($last_key => $last_val) + $original_array;
Notepad
  • 1,659
  • 1
  • 12
  • 14