0

How can I place last index as first index in an array. Suppose I have an array which looks like this.

 Array
    (
        [0] => ABC
        [1] => abc
        [2] => rodriguez
        [3] => Barkleys, 15 NO.
        [4] => A
        [5] => 1234567890
        [6] => 
        [7] => YES
        [8] => 
        [9] => 1
    )

Now I want to show this array like this.

 Array
    (
        [0] => 1
        [1] => ABC
        [2] => abc
        [3] => rodriguez
        [4] => Barkleys, 15 NO.
        [5] => A
        [6] => 1234567890
        [7] => 
        [8] => YES
        [9] => 
      )

How can I get this Please suggest.

Ramkishan Suthar
  • 403
  • 1
  • 7
  • 26
  • 1
    Please look at the accepted answer here;http://stackoverflow.com/questions/16358391/associative-array-move-last-element-to-first – TedRed Mar 06 '17 at 04:35

2 Answers2

2

Try this as an alternative way,

array_unshift($arr, $arr[count($arr)-1]);
unset($arr[count($arr)-1]);
$arr = array_values($arr);
print_r($arr);

Give it a try, this should work.

Rahul
  • 18,271
  • 7
  • 41
  • 60
0

You can also do this

$ar = array('a', 'b', 'c');

//pop out the last index from the array
$shift = (array_pop($ar));

//prepend it to the beginning
array_unshift($ar, $shift);
print_r($ar);

Output:

Array ( 
[0] => c
[1] => a 
[2] => b
)
draGon
  • 1
  • 2