Ramkishan Suthar
Ramkishan Suthar

Reputation: 403

How to place last index as first index in array php?

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.

Upvotes: 0

Views: 58

Answers (2)

draGon
draGon

Reputation: 1

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
)

Upvotes: 0

Rahul
Rahul

Reputation: 18557

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.

Upvotes: 2

Related Questions