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.

1

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Welcome mate !!
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
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.