1
1=>america,2=>India,3=>england

Above is my associative array. How can I bring 3=>england to front of the array?

3
  • As the variety of answers show, there's more than one way to do this specific case. It would help to know what you are actually trying to achieve - do you want the one with the highest numerical index at the front or do you just move the last element to the front or do you want '3 => england' to the front wherever it is in the array? Or something else entirely? Commented May 5, 2010 at 11:05
  • 2
    This is not an associative array it's an indexed array, although in paractice, PHP imlpements indexed arrays as associative. Commented May 5, 2010 at 11:50
  • This question is ambiguous about whether the original keys need to be preserved or if the array keys should be re-assigned starting from 1. Commented Feb 1, 2024 at 2:14

9 Answers 9

3

Use array_pop and array_unshift.

$lastItem = array_pop($array);
array_unshift($array, $lastItem);
Sign up to request clarification or add additional context in comments.

Comments

2

You can use the array_unshift function for that.

$array = array('americ', 'India');
array_unshift($array, 'englans');
print_r($array);

Output:

Array
(
    [0] => englans
    [1] => americ
    [2] => India
)

Comments

2

If you want to preserve the array keys use array_slice(,,,TRUE).

$array = array_slice( $array, -1, 1, TRUE ) + array_slice( $array, 0, -1, TRUE );

Comments

1
$temp = myArray[3];
$myArray[3] = $myArray[2];
$myArray[2] = $myArray[1];
$myArray[1] = $temp;

Comments

1

You can do that with array_reverse, docs you can find at http://php.net/manual/en/function.array-reverse.php

Comments

1
krsort($myArray, SORT_NUMERIC)

Comments

1

You can use array_pop and array_unshift for this:

$last = array_pop($array);
array_unshift($array, $last);

Comments

1

I think he wants to have element 3=>england to the front so he can use it with foreach and the rest of the array has to stay on the same place

than he wants this result

$array[1] = 'america';
$array[2] = 'India';
$array[3] = 'england';
$new_array[3] = $array[3];
$new_array[1] = $array[1];
$new_array[2] = $array[2];
print_r($new_array);

there is probably a function for but i cant find it so i made one

function placeLastToFirst($array){
    $newArray = array();
    $newArray[count($array)] = $array[count($array)];
    for($i = 1;$i < count($array);$i++){

        $newArray[$i] = $array[$i ];
    }
    return $newArray;
}

you have to look out because this function will only work if the array begins with 1 (normal arrays begin with 0). In that case you can use this one

function placeLastToFirst($array){
    $newArray = array();
    $newArray[count($array)-1] = $array[count($array)-1];
    for($i = 0;$i < count($array)-1;$i++){

        $newArray[$i] = $array[$i];
    }
    return $newArray;
}

Comments

0
$contractTypes = array('' => 'All') + $contractTypes;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.