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
)

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

Tags:

Php