Question: I have a PHP array similar to this:

```
0 => "red",
1 => "green",
2 => "blue",
3 => "yellow"

```

I want to move yellow to index 0. How do I do this?

Edit: My question is how do I move any one of these elements to the beginning? How would I move green to index 0, or blue to index 0? This question is not exclusively about moving the last element to the beginning.

Answer: 
This is very similar to SharpC's answer but accounts for the fact that you may not know where the value is in the array (it's key) or if it is even set. The 'if' check will skip over it if the color isn't set or if it's already the first element in the array.

```
$color = 'yellow';
$color_array = array("red", "green", "blue", "yellow");

$key = array_search($color, $color_array);

if ($key > 0) {
   unset($color_array[$key]);
   array_unshift($color_array, $color);
}

```
PHP: move any element to the first or any position:

```
$sourceArray = array(
    0 => "red",
    1 => "green",
    2 => "blue",
    3 => "yellow"
);
// set new order
$orderArray = array(
    3 => '',
    1 => '',
);
$result = array_replace($orderArray, $sourceArray);
print_r($result);

```